Skip to content

Instantly share code, notes, and snippets.

@beatak
Created September 1, 2011 23:17
Show Gist options
  • Save beatak/1187550 to your computer and use it in GitHub Desktop.
Save beatak/1187550 to your computer and use it in GitHub Desktop.
Uniquify array
/**
* @param arr {Array}
* @returns {Array} a new array of sorted, unique elements of give array.
*/
var uniquify = function (arr) {
arr.sort();
var result = [];
for (var i = 0, len = arr.length; i < len; ++i) {
if (i === 0) {
result[result.length] = arr[i];
}
else if (arr[i - 1] !== arr[i]) {
result[result.length] = arr[i];
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment