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 ;)
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
My code wouldn't have been that concise but exact solution I thought of
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One clever solution is to use Set as they have unique values by design:
It's not really an algorithm, tho ;)
Right... I always forget about sets!
That makes everything so much easier.
... and a lazy solution would be to use underscorejs:
My code wouldn't have been that concise but exact solution I thought of