Discussion on: Tackling Algorithms: Counting Unique Values

gmartigny profile image
Guillaume Martigny

One clever solution is to use Set as they have unique values by design:

function countUniqueValues(arr) {
  return new Set(arr).size;
countUniqueValues([]); // => 0
countUniqueValues([1, 1, 1, 2, 3, 3]); // => 3

It's not really an algorithm, tho ;)

denisepen profile image
Denise Pen Author

Right... I always forget about sets!

That makes everything so much easier.

anndd profile image
Anna Su**

... and a lazy solution would be to use underscorejs:

_.uniq([1, 1, 1, 2, 3, 3]).length; //=> 3
jlarrieux profile image
Jean-Rodney Larrieux

My code wouldn't have been that concise but exact solution I thought of