Created
September 1, 2011 23:17
-
-
Save beatak/1187550 to your computer and use it in GitHub Desktop.
Uniquify array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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