Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created November 6, 2012 21:31
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 abozhilov/4027724 to your computer and use it in GitHub Desktop.
Save abozhilov/4027724 to your computer and use it in GitHub Desktop.
Reordering unique
function unique(sortedArr) {
var arr = [],
i = -1,
j = -1,
len = sortedArr.length;
while (++i < len) {
if (arr[j] !== sortedArr[i]) {
arr[++j] = sortedArr[i];
}
}
return arr;
}
console.log(unique([1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 6]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment