This seems like such a simple need but I've spent an inordinate amount of time trying to do this to no avail. I've looked at other questions on SO and I haven't found what I need. I have a very simple JavaScript array such as peoplenames = new Array("Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl");
that may or may not contain duplicates and I need to simply remove the duplicates and put the unique values in a new array. That's it. I could point to all the codes that I've tried but I think it's useless because they don't work. If anyone has done this and can help me out I'd really appreciate it. JavaScript or jQuery solutions are both acceptable.
|
||||
Quick and dirty using jQuery:
|
|||||||||||||||||||||
|
"Smart" but naïve way
Basically, we iterate over the array and, for each element, check if the first position of this element in the array is equal to the current position. Obviously, these two positions are different for duplicate elements. Using the 3rd ("this array") parameter of the filter callback we can avoid a closure of the array variable:
Although concise, this algorithm is not particularly efficient for large arrays (quadratic time). Hashtables to the rescue
This is how it's usually done. The idea is to place each element in a hashtable and then check for its presence instantly. This gives us linear time, but has at least two drawbacks:
That said, if your arrays contain only primitives and you don't care about types (e.g. it's always numbers), this solution is optimal. The best from two worldsAn universal solution combines both approaches: it uses hash lookups for primitives and linear search for objects.
sort | uniqAnother option is to sort the array first, and then remove each element equal to the preceding one:
Again, this doesn't work with objects (because all objects are equal for Unique by...Sometimes it's desired to uniquify a list based on some criteria other than just equality, for example, to filter out objects that are different, but share some property. This can be done elegantly by passing a callback, usually called a "key".
A particularly useful
|
|||||||||||||||||||||
|
Use Underscore.jsIt's a library with a host of functions for manipulating arrays.
Note: Lo-Dash (an underscore competitor) also offers a comparable .uniq implementation. |
||||
|
Got tired of seeing all bad examples with for-loops or jQuery. Javascript has the perfect tools for this nowadays: sort, map and reduce. Uniq reduce while keeping existing order
Faster uniq with sortingThere are probably faster ways but this one is pretty decent.
|
||||
You can always try putting it into an object, and then iterating through its keys:
|
|||||
|
A single line version using array filter and indexOf functions:
|
|||
|
The top answers have complexity of
This will work for strings, numbers, and dates. If your array contains complex objects (ie, they have to be compared with
|
|||||||||||||||||||||
|
The following is more than 80% faster than the jQuery method listed (see tests below). It is an answer from a similar question a few years ago, if I come across the person who originally proposed it I will post credit. Pure JS.
My Test Case comparison: http://jsperf.com/remove-duplicate-array-tests |
|||||||||||||||||
|
You could also use the However, the code there isn’t very well written, since it declares the |
|||
|
|
||||
|
A slight modification of thg435's excellent answer to use a custom comparator:
|
|||
|
This is probably one of the fastest way to remove permanently the duplicates from an array 10x times faster than the most functions here.& 78x faster in safari
if you can't read the code above ask, read a javascript book or here are some explainations about shorter code. http://stackoverflow.com/a/21353032/2450730 |
|||||
|
Here is another approach using jQuery,
Author: William Skidmore |
|||
|
|
|||
|
If you don't want to include a whole library, you can use this one off to add a method that any array can use:
|
|||
|
If you're creating the array yourself, you can save yourself a loop and the extra unique filter by doing the check as you're inserting the data;
|
|||
|
peoplenames = ["Mike",...];
– mgraph Feb 10 '12 at 15:01_.uniq(peoplenames)
solves this lodash.com/docs#uniq – Connor Leech Jul 29 at 19:29