I need to write a function to find all the elements that appear more than once in the array and then return those elements in an array. Example: Input: [2,7,4,10,12,27,4,7,7,12, 10] Output: [4,7,10,12]
This is what i have so far:
let arr= [4,4,6,8,8,9,10,10]
var method1 = function(a) {
var counts = [];
for(var i = 0; i <= a.length; i++) {
for(var j = i; j <= a.length; j++) {
if(i != j && a[i] == a[j]) {
counts.push(a[i]);
}
}
}
return counts;
}
console.log(method1(arr));
It works when the array only have two of a number, but not if there are more. How can I push to counts only one of each number independently of how many duplicates there are?
thanks for any help!