We have a Javascript array and we want to find the all non-unique elements of the array. To get the all non-unique values from the array here are few examples.
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 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 end of array will be selected. Negative numbers are used to select from the end of array.
Example-1: This example first sort the array and then selects them one by one if they are non-unique.
<!DOCTYPE html> <html> <head> <title> JavaScript | Get all non-unique values. </title> </head> <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> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2:This example make a dictionary and then store the frequency of each element. Later if they found out to be frequency more than 1, then the element will be selected.
<!DOCTYPE html> <html> <head> <title> JavaScript | Get all non-unique values. </title> </head> <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> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to get all unique values (remove duplicates) in a JavaScript array?
- How to get all property values of a JavaScript Object (without knowing the keys) ?
- How to get values from html input array using JavaScript ?
- How to get the javascript function parameter names/values dynamically?
- How to check all values of an array are equal or not in JavaScript ?
- Find all the combinations of the array values in JavaScript
- How to get the elements of one array which are not present in another array using JavaScript?
- How to get multiple selected values of select box in php?
- JavaScript | array.values()
- How to create an array with random values with the help of JavaScript ?
- How to Create a Dropdown List with Array Values using JavaScript ?
- How to get all the methods of an object using JavaScript ?
- How to get all ID of the DOM elements with JavaScript ?
- How to get all CSS styles that are applied directly to an element using JavaScript ?
- How to print all the values of an array in PHP ?
- How to get character array from string in JavaScript?
- Get the first and last item in an array using JavaScript
- How to get a list of associative array keys in JavaScript ?
- How to get array structure with alert() in JavaScript?
- How to get the longest string in an array using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


