LeetCode in Ruby: 26. Remove Duplicates from Sorted Array

twitter logo github logo ・1 min read

LeetCode in Ruby (11 Part Series)

1) LeetCode in Ruby: 70. Climbing Stairs 2) LeetCode in Ruby: 206 Reverse Linked List 3 ... 9 3) LeetCode in Ruby: 102. Binary Tree Level Order Traversal 4) LeetCode in Ruby: 107. Binary Tree Level Order Traversal II 5) LeetCode in Ruby: 70. Climbing Stairs 6) LeetCode in Ruby: 242. Valid Anagram 7) LeetCode in Ruby: 387. First Unique Character in a String 8) LeetCode in Ruby: 771 Jewels and Stones 9) LeetCode in Ruby: 56. Merge Intervals 10) LeetCode in Ruby: 26. Remove Duplicates from Sorted Array 11) LeetCode in Ruby: 62. Unique Paths
def remove_duplicates(nums)
  nums.uniq!
  return nums.length
end

First, use uniq! to remove all the duplicates in nums. Then return its length.

Time complexity: O(n)

Extra memory: O(1)

twitter logo DISCUSS (1)
Classic DEV Post from Aug 23

Favorite Front-end/UI developer interview questions?

Help me compile your favorite interview questions.

Kaitian Xie profile image