loading...

LeetCode in Ruby: 26. Remove Duplicates from Sorted Array

twitter logo github logo Updated on ・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 Mar 29 '19

Speed of Voice - The Pace of Your Presentation Can Heavily Impact How Well it Lands

Learn how to control the pace of your voice in your presentation to land your message

Kaitian Xie profile image