Skip to content

Instantly share code, notes, and snippets.

@phoetry
Forked from 140bytes/LICENSE.txt
Created December 2, 2011 12:07
Show Gist options
  • Select an option

  • Save phoetry/1423014 to your computer and use it in GitHub Desktop.

Select an option

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

nikola commented Dec 2, 2011

Copy link
Copy Markdown

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

phoetry commented Dec 2, 2011

Copy link
Copy Markdown
Author

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

@tsaniel

tsaniel commented Dec 3, 2011

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

ES5 version.

@jed

jed commented Dec 4, 2011

Copy link
Copy Markdown

@tsaniel

tsaniel commented Dec 4, 2011

Copy link
Copy Markdown

So what about mine?

@jed

jed commented Dec 4, 2011

Copy link
Copy Markdown

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

@tsaniel

tsaniel commented Dec 5, 2011

Copy link
Copy Markdown

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

@nikola

nikola commented Dec 5, 2011

Copy link
Copy Markdown

Array.filter() is ES5 according to MDN.

@jed

jed commented Dec 5, 2011

Copy link
Copy Markdown

true, but @atk's version is not.

@nikola

nikola commented Dec 5, 2011

Copy link
Copy Markdown

Link?

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