Below is a code snippet which finds out "How many times each element of an array is repeated". Everything is working as expected except it doesn't give me "ebay repetition count"
var strArr = ["google","ebay","yahoo","google","ebay","facebook","facebook","google"];
var output= {};
strArr.forEach(function(element) {
var count = 0;
try{
while(strArr.indexOf(element) !== -1){
strArr.splice(strArr.indexOf(element),1);
count++;
}
}catch(e){
console.log(e);
}
output[element] = count;
});
console.log(output);
I tried debugging it with debugger;
, I figured out that the second element of the array is being skipped.
Expected Output:
google:3
ebay:2
yahoo:1
facebook:2
Actual Output:
google:3
yahoo:1
facebook:2