Created
March 20, 2018 15:10
-
-
Save lkrych/3e073408c8a26cfb4e2174650c1c7f60 to your computer and use it in GitHub Desktop.
more optimized remove dups
This file contains hidden or 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
//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