Skip to content

Commit 15e6200

Browse files
committed
add: 26 removeDuplicates in swift
1 parent a4d1caa commit 15e6200

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

programming-swift/26.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// take list of numbers and return length of non duplicate numbers
2+
// The judge will test your solution with the following code:
3+
4+
// int[] nums = [...]; // Input array
5+
// int[] expectedNums = [...]; // The expected answer with correct length
6+
7+
// int k = removeDuplicates(nums); // Calls your implementation
8+
9+
// assert k == expectedNums.length;
10+
// for (int i = 0; i < k; i++) {
11+
// assert nums[i] == expectedNums[i];
12+
// }
13+
14+
func removeDuplicated(nums: inout [Int])-> Int{
15+
var uniqueUptoIndex: Int = 0;
16+
for index: Int in 1..<nums.count{
17+
if(nums[uniqueUptoIndex] != nums[index]){
18+
uniqueUptoIndex += 1;
19+
nums[uniqueUptoIndex] = nums[index];
20+
}
21+
}
22+
return uniqueUptoIndex+1
23+
}
24+
25+
26+
// to test
27+
var sample1 = [0,0,1,2,3]
28+
var sample2: [Int] = [1,2,3];
29+
print(removeDuplicated(nums: &sample1)) // should be 4
30+
print(removeDuplicated(nums: &sample2)) // should be 3

0 commit comments

Comments
 (0)