10-Nov-2020
.
Admin
Hi Guys,
In this example,I will learn you how to to get all non-unique values from an array in javascript.you can easy simply get all non-unique values from an array in javascript.
This example first sort the array and then selects them one by one if they are non-unique.
Example:
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript | Get all non-unique values.
</title>
</head>
<body style="text-align:center;" id="body">
<h2 style="color: #af1bf9;">
NiceSnippets
</h2>
<p id="nsnup" style="font-size: 16px;">
</p>
<button onclick="gfg_Run()">
click here
</button>
<p id="nsndown" style="color:#af1bf9; font-size: 20px; font-weight: bold;">
</p>
<script>
var ns_up = document.getElementById("nsnup");
var ns_down = document.getElementById("nsndown");
var nsarr = [26, 42, 25, 5, 8, 45];
var nssort_arr = nsarr.slice().sort();
var nsres = [];
ns_up.innerHTML = "Original Array = [" + nsarr + "]";;
function gfg_Run() {
for (var i = 0; i < nssort_arr.length - 1; i++) {
if (nssort_arr[i + 1] == nssort_arr[i]) {
nsres.push(nssort_arr[i]);
}
}
ns_down.innerHTML = "Non-Unique values are = " + nsres;
}
</script>
</body>
</html>
It will help you...
#JavaScript