Skip to main content
53 events
when toggle format what by license comment
S Apr 22 at 11:26 history suggested Levi_OP CC BY-SA 4.0
update url to reflect current url for the Standard ECMA-262
Apr 19 at 17:56 review Suggested edits
S Apr 22 at 11:26
S Sep 1, 2023 at 19:15 history suggested Mark Wiemer CC BY-SA 4.0
Add 2023 answer using `Array.some` and predicate function variable
Aug 29, 2023 at 22:59 review Suggested edits
S Sep 1, 2023 at 19:15
Apr 21, 2023 at 8:23 comment added etwas77 lodash _.uniq makes the trick of uniqueness
Jun 28, 2022 at 16:20 history edited AntoineB CC BY-SA 4.0
I lost 30 min because I assumed the method was modifying the array in place due to the code example not being explicit, so making this example explicit.
Apr 8, 2022 at 13:52 comment added Vince It doesn't remove duplicates
Feb 4, 2022 at 14:56 comment added Greggory Wiley Any danger in using the es6 version this way? templistarray = [...new Set([...list,...templistarray])];
May 29, 2021 at 4:20 history rollback user229044
Rollback to Revision 15
Apr 28, 2021 at 5:32 history edited dthree CC BY-SA 4.0
Added interactive examples
Feb 12, 2021 at 19:28 comment added LiraNuna Please understand that this answer was written over 11 years ago, when things like forEach and other nice things we're used to today did not exist.
Feb 7, 2021 at 0:38 comment added Jannis Ioannou I don't like for. So I will prefer: simo's answer.
Sep 4, 2020 at 10:06 comment added gdenuf Just to add on top of original answer using ES6 filter/include, ignoring all big o comments: const array3 = [...array1, ...(array2.filter(v => !array1.includes(v)))];
Jul 6, 2020 at 17:14 comment added ggorlen O(n^2) can be faster if the arrays are small because creating a Set object has some constant factor overhead due to allocation and GC. But this answer makes no mention that the algorithm is not scalable beyond small arrays. Even if you do need an O(n^2) algorithm, using filter and indexOf is more idiomatic and clean. The likely reason this answer is so highly upvoted is because it came on the stage first and got accepted back in 2009, but in nearly every respect it's a poor solution.
Jun 20, 2020 at 9:12 history edited CommunityBot
Commonmark migration
S Sep 20, 2019 at 1:08 history suggested dota2pro CC BY-SA 4.0
code should be put into snippet
Sep 19, 2019 at 23:49 review Suggested edits
S Sep 20, 2019 at 1:08
Jul 3, 2019 at 18:31 history rollback LiraNuna
Rollback to Revision 11
Jul 3, 2019 at 9:10 history edited Black CC BY-SA 4.0
added 142 characters in body
May 28, 2019 at 11:31 review Suggested edits
May 28, 2019 at 12:32
Apr 5, 2019 at 16:08 review Suggested edits
Apr 5, 2019 at 16:35
Jan 4, 2019 at 22:53 comment added Turbo @AmrAli, your link is 404. jsperf.com/merge-two-arrays-keeping-only-unique-values by slickplaid works and shows this is the worst performing answer.
Mar 29, 2018 at 16:45 history edited Bruno João CC BY-SA 3.0
Added ES6 approach.
Dec 27, 2017 at 6:14 review Suggested edits
Dec 27, 2017 at 7:49
Oct 20, 2017 at 21:30 history edited TylerH CC BY-SA 3.0
All browsers support ES5 now, so it's no longer 'progressive'
Oct 6, 2017 at 14:42 comment added Amr Ali For other methods to dedup an array, please see my benchmarks at: jsperf.com/de-duplicate-an-array-keeping-only-unique-values
Jun 9, 2017 at 18:27 vote accept Vijjendra
Apr 13, 2017 at 21:11 comment added Gerard ONeill Stelios -- that is still O(n^2) -- you are just hiding the outer loop in a system function. You need to go merge or hash or binary search
Dec 2, 2016 at 12:25 comment added Stelios Voskos @LiraNuna I would replace the O(n^2) solution for the duplicate removal with the filter function. It should accept two parameters: the element and the index.
S Aug 12, 2016 at 18:08 history suggested David Furlong CC BY-SA 3.0
Fixed typo in code (Added other text as edits must be 6 chs)
Aug 12, 2016 at 17:35 review Suggested edits
S Aug 12, 2016 at 18:08
Aug 11, 2016 at 1:28 review Suggested edits
Aug 11, 2016 at 3:05
May 4, 2016 at 20:32 comment added cmcculloh Just use Babel and Set() as described in another answer.
Oct 8, 2015 at 16:11 history edited Christophe Roussy CC BY-SA 3.0
removed unnessary semicolon at end of function.
Jun 21, 2014 at 15:04 comment added riv @DiegoNunes: your solution is still O(n^2) because removing an item from the array is O(n). Besides, table lookup is probably O(log n) so even fixing that won't make it linear.
Jan 11, 2014 at 21:10 history edited om-nom-nom CC BY-SA 3.0
this way result order will be obvious
Nov 11, 2013 at 4:43 history edited Qantas 94 Heavy CC BY-SA 3.0
Made link direct
Sep 8, 2013 at 0:57 comment added diego nunes You can do the "unique" bit in O(n) using something like var b=[]; for (var i=0,n=a.length;i<a;i++) { if (typeof(b[a[i]]) === 'undefined') { b[a[i]] = true; continue; } a.splice(i--, 1); }.
Jul 1, 2013 at 13:42 comment added Tjorriemorrie Does this work with classes in the array? e.g. questions: [Class, Class] after concat gives me []
Jan 31, 2013 at 5:25 history edited LiraNuna CC BY-SA 3.0
added 6 characters in body
Jan 30, 2013 at 7:18 comment added Jan Zyka just to note that the arrayUnique function doesn't really work for arrays which has more than two identical values ...
Jan 1, 2013 at 12:17 comment added mulllhausen you should always use for ... in with hasOwnProperty in which case the prototype method is fine
Dec 15, 2012 at 0:02 history edited LiraNuna CC BY-SA 3.0
added 1225 characters in body
Dec 14, 2012 at 6:52 comment added Camilo Martin @CodeCommander "but people often use them that way" - novice or bad programmers use it that way, that's why you have conventions. I used for in in arrays before, but I'd never do that again. Even some frameworks augment prototypes, so you shouldn't really care for breaking for in in arrays (and especially, you should never use it yourself!).
Feb 21, 2012 at 4:55 review Suggested edits
Feb 21, 2012 at 5:12
Feb 2, 2011 at 0:49 comment added Code Commander I originally up-voted this but have changed my mind. Assigning prototypes to Array.prototype has the consequences of breaking "for ... in" statements. So the best solution is probably to use a function like this but not assign it as a prototype. Some people may argue that "for ... in" statements shouldn't be used to iterate array elements anyway, but people often use them that way so at the very least this solution be used with caution.
Oct 18, 2009 at 13:26 vote accept Vijjendra
Jun 9, 2017 at 18:27
Oct 18, 2009 at 11:16 comment added Amarghosh But I think it's worth it. I learned that w3schools is not the best reference out there, that indexOf is in fact present in js and how to add it for older browsers, and a new use for the in keyword (from in this part in the MDC version of indexOf, I didn't know that).
Oct 18, 2009 at 10:59 history edited LiraNuna CC BY-SA 2.5
The things coding at 4am will do to you...
Oct 18, 2009 at 10:40 history edited LiraNuna CC BY-SA 2.5
Fixed order; added 5 characters in body
Oct 18, 2009 at 9:04 comment added Amarghosh Let [a, b, c] and [x, b, d] be the arrays (assume quotes). concat gives [a, b, c, x, b, d]. Wouldn't the unique()'s output be [a, c, x, b, d]. That doesn't preserve the order I think - I believe OP wants [a, b, c, x, d]
Oct 18, 2009 at 8:54 comment added Gumbo Note that this algorithm is O(n^2).
Oct 18, 2009 at 8:42 history answered LiraNuna CC BY-SA 2.5