0

I have a 2D array of number arrays like say

nonunique = [[1,2,3][2,3,4][1,5]];

I want a 1d array of all the unique keywords.

unique = [1,2,3,4,5];

Existing solutions aim at matching the entire arrays rather than just returning a 1d array of numbers. Any shortcuts to this in vanilla javascript?

I tried this reduce function from stackoverflow but it seems to filter some non-duplicate values too.

unique = nonunique.reduce((acc, val) => acc.concat(val.filter((e)=>acc.includes(val))));

It doesn't work properly and I can't figure out how to fix it. I also tried

unique = nonunique.filter(onlyUnique);

but filter(onlyUnique) doesn't seem to be available on firefox.

xmxmxmx
  • 409
  • 1
  • 6
  • 16

0 Answers0