Created
January 19, 2020 12:41
-
-
Save spurscho/9652cbb2e4c3d9f6689b4c2f05fbd92e to your computer and use it in GitHub Desktop.
26. Remove Duplicates from Sorted Array
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
| class Solution { | |
| func removeDuplicates(_ nums: inout [Int]) -> Int { | |
| guard nums.count > 0 else { | |
| return 0 | |
| } | |
| nums.sort() | |
| var idx: Int = 0 | |
| while true { | |
| if idx + 1 == nums.count { | |
| break | |
| } | |
| if nums[idx] == nums[idx+1] { | |
| nums.remove(at: idx) | |
| } else { | |
| idx += 1 | |
| } | |
| } | |
| return nums.count | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment