JavaScript Get all non-unique values from an array
We have a Javascript array and we want to find the all non-unique elements of the array. To get all non-unique values from the array here are a few examples.
JavaScript Array Slice() Method: This method returns the selected elements in a new array object. This method gets the elements starting from the specified start argument and ends at excluding the given end argument.
Syntax:
array.slice(start, end)
Parameters:
- start: This parameter is optional. It specifies the integer from where to start the selection (index starts from 0). Negative numbers are used to select from the end of the array. If not used, it acts like “0”.
- end: This parameter is optional. It specifies the integer from where the end of the selection. If not used all elements from the start to the end of the array will be selected. Negative numbers are used to select from the end of the array.
Example 1: This example first sorts the array and then selects them one by one if they are non-unique.
HTML
< body style = "text-align:center;" id = "body" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px;" > </ p > < button onclick = "gfg_Run()" > click here </ button > < p id = "GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var arr = [89, 89, 11, 2, 3, 4, 2, 4, 5, 7]; var sort_arr = arr.slice().sort(); var res = []; el_up.innerHTML = "Original Array = [" + arr + "]";; function gfg_Run() { for (var i = 0; i < sort_arr.length - 1; i++) { if (sort_arr[i + 1] == sort_arr[i]) { res.push(sort_arr[i]); } } el_down.innerHTML = "Non-Unique values are = " + res; } </script> </ body > |
Output:

Example 2: In this example make a dictionary and then store the frequency of each element. Later if they are found to be a frequency of more than 1, then the element will be selected.
HTML
< body style = "text-align:center;" id = "body" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px;" > </ p > < button onclick = "gfg_Run()" > click here </ button > < p id = "GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var arr = [89, 89, 11, 2, 3, 4, 2, 4, 5, 7]; el_up.innerHTML = "Original Array = [" + arr + "]";; function gfg_Run() { var len = arr.length, output = [], count = {}; for (var i = 0; i < len ; i++) { var item = arr [i]; count[item] = count[item] >= 1 ? count[item] + 1 : 1; if (count[item] === 2) { output.push(item); } } el_down.innerHTML = "Non-Unique values are = " + output; } </ script > </ body > |
Output:

Example 3: This example uses the filter method which checks the index of elements varies for elements any element and returns elements.
HTML
< body style = "text-align:center;" id = "body" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px;" > </ p > < button onclick = "gfg_Run()" > click here </ button > < p id = "GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var arr = [89, 89, 11, 2, 3, 4, 2, 4, 5, 7]; el_up.innerHTML = "Original Array = [" + arr + "]";; function gfg_Run() { output = arr.filter((item, index )=> arr.indexOf(item) !== index ); el_down.innerHTML = "Non-Unique values are = " + output; } </ script > </ body > |
Output:

Array.filter method
Please Login to comment...