Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

22
  • 382
    Note that this algorithm is O(n^2).
    – Gumbo
    Commented Oct 18, 2009 at 8:54
  • 9
    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]
    – Amarghosh
    Commented Oct 18, 2009 at 9:04
  • 10
    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. Commented Feb 2, 2011 at 0:49
  • 17
    you should always use for ... in with hasOwnProperty in which case the prototype method is fine Commented Jan 1, 2013 at 12:17
  • 2
    Just use Babel and Set() as described in another answer.
    – cmcculloh
    Commented May 4, 2016 at 20:32