Example 1: count duplicates array js
uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"];
var count = {};
uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;});
console.log(count);
Example 2: how to check for duplicate syntax in javascript
let strArray = [ "q", "w", "w", "w", "e", "i", "u", "r"];
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
console.log(findDuplicates(strArray))
console.log([...new Set(findDuplicates(strArray))])
Example 3: find duplicates and their count in an array javascript
var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
Example 4: javascript find duplicate in array
const isThereADuplicate = function(arrayOfNumbers) {
let counts = {};
for(var i = 0; i <= arrayOfNumbers.length; i++) {
if(counts[arrayOfNumbers[i]] === undefined) {
counts[arrayOfNumbers[i]] = 1;
} else {
return true;
}
}
return false;
}
Example 5: javascript get duplicates in array
function getDuplicateArrayElements(arr){
var sorted_arr = arr.slice().sort();
var results = [];
for (var i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] === sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
var colors = ["red","orange","blue","green","red","blue"];
var duplicateColors= getDuplicateArrayElements(colors);
Example 6: js find duplicates in array
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i)