Skip to content

Instantly share code, notes, and snippets.

@phoetry
Forked from 140bytes/LICENSE.txt
Created December 2, 2011 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phoetry/1423014 to your computer and use it in GitHub Desktop.
Save phoetry/1423014 to your computer and use it in GitHub Desktop.
Cross-browser Array.prototype.unique
Array.prototype.unique =
function()
{
for(var
a = this.slice().sort(), // sort this array
c = a.length, // length of sorted array
b = []; // result array
c--; // loop
a[c] === a[c-1] || // if current item is equal with neighbor, do not
b.push(a[c]) // push it
);
return b; // return the result
}
// 122 bytes
Array.prototype.unique=function(){for(var a=this.slice().sort(),b=[],c=a.length;c--;a[c]===a[c-1]||b.push(a[c]));return b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "Cross-browser Array.prototype.unique",
"keywords": [
"array",
"unique",
"sort"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
Array.prototype.unique=function(){for(var a=this.slice().sort(),b=[],c=a.length;c--;a[c]===a[c-1]||b.push(a[c]));return b}
var arr = ['f','h','j','s','h','f','j','s','h','f','g','a','h','s','f','i','a','h','f','i','w','a','o','h','f','p','a','f','h','d','s','f','j','h'];
document.getElementById( "ret" ).innerHTML = arr.unique().join(', ');
</script>
@nikola
Copy link

nikola commented Dec 2, 2011

Unfortunately, this implementation is not stable, i.e. items do not preserve order. Also, it only works when all items are of primitive type, i.e. it won't work with nested arrays etc.

Not sure if this is useful in the current state.

@phoetry
Copy link
Author

phoetry commented Dec 2, 2011

Thanks @nikola, you are right, this script disrupts the order.

@tsaniel
Copy link

tsaniel commented Dec 3, 2011

Array.prototype.unique=function f(a,b,c){return c?b==c.indexOf(a):this.filter(f)}

ES5 version.

@jed
Copy link

jed commented Dec 4, 2011

@tsaniel
Copy link

tsaniel commented Dec 4, 2011

So what about mine?

@jed
Copy link

jed commented Dec 4, 2011

for 140byt.es, we usually assume only ES3. ES5 makes it too easy!

@tsaniel
Copy link

tsaniel commented Dec 5, 2011

@jed: But isn't yours uses ES5 also?

@nikola
Copy link

nikola commented Dec 5, 2011

Array.filter() is ES5 according to MDN.

@jed
Copy link

jed commented Dec 5, 2011

true, but @atk's version is not.

@nikola
Copy link

nikola commented Dec 5, 2011

Link?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment