0

I have an array which has duplicated values and I want to filter that array with the only unique elements. All values which have been duplicated or occurring more than 1 times should be excluded

Example: I have an array x which has these values

x = [1, 2, 3, 4, 2, 3]

Expected Result is

[1, 4]
Kiel
  • 235
  • 2
  • 8
  • I'd feed it into a Set. – Fallenreaper Jun 10 '19 at 17:21
  • @Fallenreaper I try that, but still not as expected. Use Set will return [1, 2, 3, 4] – Kiel Jun 10 '19 at 17:22
  • @Fallenreaper `Set` will not get the expected output. OP wants to completely remove duplicates. In other words you can say that he wants to only those elements which occur once. – Maheer Ali Jun 10 '19 at 17:23
  • Use `reduce` to create an object which has array elements as keys and values as its count and then you can apply filter whose values are equal to one. `const x = [1, 2, 3, 4, 2, 3]; const groupByOccurence = x.reduce((accu, ele) => {accu[ele] = (accu[ele] || 0) + 1; return accu;}, {});console.log(Object.entries(groupByOccurence).filter(([_, val]) => val == 1).map(([key, _]) => +key));` – random Jun 10 '19 at 17:47
  • Oops, yeah, i didnt see the delete, i thought it was "Remove Duplicates". Since it is marked a duplicate, I cant give my answer, so i would suggest looking at that other thread. – Fallenreaper Jun 10 '19 at 17:48

0 Answers0