diff --git a/Array/RemoveDuplicatesFromSortedArray.swift b/Array/RemoveDuplicatesFromSortedArray.swift index af593d3c..9e2f7036 100644 --- a/Array/RemoveDuplicatesFromSortedArray.swift +++ b/Array/RemoveDuplicatesFromSortedArray.swift @@ -6,18 +6,17 @@ */ class RemoveDuplicatesFromSortedArray { - func removeDuplicates(inout nums: [Int]) -> Int { - guard nums.count > 0 else { - return 0 - } - - var index = 0 + func removeDuplicates(_ nums: inout [Int]) -> Int { + if nums.isEmpty { return 0 } - for num in nums where num != nums[index] { - index += 1 - nums[index] = num + var i = 0 + for j in 1.. Int { - nums = nums.filter { (num) in num != val } + func removeElement(_ nums: inout [Int], _ val: Int) -> Int { + nums = nums.filter { $0 != val } return nums.count } -} \ No newline at end of file +} diff --git a/Stack/ValidParentheses.swift b/Stack/ValidParentheses.swift index 302ee9f2..4784419d 100644 --- a/Stack/ValidParentheses.swift +++ b/Stack/ValidParentheses.swift @@ -12,15 +12,15 @@ class ValidParentheses { if char == "(" || char == "[" || char == "{" { stack.append(char) } else if char == ")" { - guard stack.count != 0 && stack.removeLast() == "(" else { + guard stack.popLast() == "(" else { return false } } else if char == "]" { - guard stack.count != 0 && stack.removeLast() == "[" else { + guard stack.popLast() == "[" else { return false } } else if char == "}" { - guard stack.count != 0 && stack.removeLast() == "{" else { + guard stack.popLast() == "{" else { return false } } @@ -28,4 +28,4 @@ class ValidParentheses { return stack.isEmpty } -} \ No newline at end of file +}