uniq
uniq() -> newArray
Produces a duplicate-free version of an array. If no duplicates are found, the original array is returned.
Example
['Sam', 'Justin', 'Andrew', 'Dan', 'Sam'].uniq();
// -> ['Sam', 'Justin', 'Andrew', 'Dan']
['Prototype', 'prototype'].uniq();
// -> ['Prototype', 'prototype'] because String comparison is case-sensitive
Performance considerations
On large arrays with duplicates, this method has a potentially large performance cost:
- Since it does not require the array to be sorted, it has quadratic complexity.
- Since it relies on JavaScript’s
Array.concat
, it will yield a new, intermediary array every time it encounters a new value (a value that wasn’t already in the result array).
More efficient implementations could be devised. This page will get updated if such an optimization is committed.