Skip to content

Instantly share code, notes, and snippets.

@xianhuazhou
Created June 29, 2012 11:20
Show Gist options
  • Select an option

  • Save xianhuazhou/3017414 to your computer and use it in GitHub Desktop.

Select an option

Save xianhuazhou/3017414 to your computer and use it in GitHub Desktop.
Array::uniq = (cmp = null)->
arr = this.slice(0)
if typeof(cmp) == 'function'
arr.sort(cmp)
else
arr.sort()
i = 0
size = arr.length
while i < size
while true
j = i + 1
break if arr[j] != arr[i] or not arr[j]?
arr.splice(j, 1)
i += 1
arr
# [ 1, 2, 3, 4, 5, 6 ]
array = [1, 1, 2, 2, 5, 2, 6, 5, 2, 2, 3, 2, 3, 4]
console.log array.uniq()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment