re: Tackling Algorithms: Counting Unique Values VIEW POST

VIEW FULL DISCUSSION
 

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 ;)

 

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

 

Right... I always forget about sets!

That makes everything so much easier.

 

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

_.uniq([1, 1, 1, 2, 3, 3]).length; //=> 3
code of conduct - report abuse