lundi 26 octobre 2020

how to conosle repeating numbers of an array?

I have one array of numbers, and now I want to console numbers that are repeated many times from that array, in this case numbers 2 and 3, how do I do that?

var arr = [1,2,3,4,2,5,6,3,8];

i tried create new array and with for loop and if statement push repeated numbers in another array but that didn't work...

var arr = [1,2,3,4,2,5,6,3,8];

var arr2 = [];
var kon = [];

for(var i in arr){
  arr2.push(arr[i]);
  for(var j in arr2){
    if (arr[i] === arr2[j]){
      kon.push(arr[j]);
    }
  }
}
console.log(kon);

result: [1, 2, 3, 4, 2, 2, 5, 6, 3, 3, 8]

Aucun commentaire:

Enregistrer un commentaire