I have been asked in the interview to find repetitive elements in array. I have found using for loop, but the interviewer asked for more improved way to find out, with effect to performance with out for loop . I am beginner in exploring Java script. Could any one help to find other methods for finding repetitive elements in array. Below is my code for the answer
var a = [1,2,3,3,4,4,5,5,6,7,8,8,9,10,11,12];
var repeatElements = [];
for (var i=0;i<a.length;i++){
for(var j=1+i; j<a.length;j++){
if (a[i]===a[j]){
repeatElements.push(a[i]);
}
}
}
console.log(repeatElements);
I have checked the answer of this stack overflow question Get all unique values in a JavaScript array (remove duplicates) Whether using filter for finding repetitive would be more efficient way?