Skip to content

Instantly share code, notes, and snippets.

@lkrych
Created March 20, 2018 15:10
Show Gist options
  • Save lkrych/3e073408c8a26cfb4e2174650c1c7f60 to your computer and use it in GitHub Desktop.
Save lkrych/3e073408c8a26cfb4e2174650c1c7f60 to your computer and use it in GitHub Desktop.
more optimized remove dups
//In O(n) time, and using O(1) space, remove duplicates from an array
func removeDups(arr []int) []int {
nextUnduplicatedIdx := 1
//O(n) time complexity
for i := 1; i < len(arr); i++ {
//if the adjacent elements are not equal, assign the element at i to our nextUnduplicatedIdx
if arr[nextUnduplicatedIdx-1] != arr[i] {
arr[nextUnduplicatedIdx] = arr[i]
nextUnduplicatedIdx++
}
}
return arr[:nextUnduplicatedIdx]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment