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 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