0

I have an array, which contains duplicate values. How can I push duplicates in to another array?

let arr1 = [1, 5, 3, 6, 9, 5, 1, 4, 2, 7, 9], and duplicates array should be dupArr = [1, 5, 9]

Tatevik
  • 375
  • 1
  • 4
  • 11
  • 5
    This is fairly simple and can be done in various ways, what have you tried? – apomene Oct 09 '18 at 12:30
  • Possible Duplicate https://stackoverflow.com/questions/47132232/how-to-keep-duplicates-of-an-array – chintuyadavsara Oct 09 '18 at 12:35
  • 3
    Possible duplicate of [How to keep Duplicates of an Array](https://stackoverflow.com/questions/47132232/how-to-keep-duplicates-of-an-array) – Daniel Beck Oct 09 '18 at 12:35
  • I have two arrays, which I get from my reducer (one of them contains second array's some datas), and I want to mark with colored star those items, which I have in both arrays, and mark others with white star. – Tatevik Oct 09 '18 at 12:42
  • @DanielBeck thanks a lot) – Tatevik Oct 09 '18 at 12:45

1 Answers1

0

You could filter the array by storing the previous checked values in a Set, which is here a closure.

var array = [1, 5, 3, 6, 9, 5, 1, 4, 2, 7, 9],
    duplicates = array.filter((s => v => s.has(v) || !s.add(v))(new Set));
    
console.log(duplicates);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392