✒️
Leetcode
  • Leetcode
  • Ruby
    • 1. Two Sum
    • 7. Reverse Integer
    • 9. Palindrome Number
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 20. Valid Parentheses
    • 21. Merge Two Sorted Lists
    • 26. Remove Duplicates from Sorted Array
    • 27. Remove Element
    • 28. Implement strStr()
    • 35. Search Insert Position
    • 38. Count and Say
    • 53. Maximum Subarray
    • 58. Length of Last Word
    • 66. Plus One
    • 67. Add Binary
    • 69. Sqrt(x)
    • 70. Climbing Stairs
    • 83. Remove Duplicates from Sorted List
    • 88. Merge Sorted Array
    • 100. Same Tree
    • 101. Symmetric Tree
    • 104. Maximum Depth of Binary Tree
    • 121. Best Time to Buy and Sell Stock
    • 122. Best Time to Buy and Sell Stock II
    • 125. Valid Palindrome
    • 136. Single Number
    • 141. Linked List Cycle
    • 155. Min Stack
    • 160. Intersection of Two Linked Lists
    • 169. Majority Element
    • 191. Number of 1 Bits
    • 198. House Robber
    • 203. Remove Linked List Elements
    • 205. Isomorphic Strings
    • 206. Reverse Linked List
    • 217. Contains Duplicate
    • 226. Invert Binary Tree
    • 231. Power of Two
    • 234. Palindrome Linked List
    • 263. Ugly Number
    • 283. Move Zeroes
    • 292. Nim Game
    • 344. Reverse String
    • 350. Intersection of Two Arrays II
    • 371. Sum of Two Integers
    • 387. First Unique Character in a String
    • 404. Sum of Left Leaves
    • 401. Binary Watch
    • 448. Find All Numbers Disappeared in an Array
    • 922. Sort Array By Parity II
    • 1051. Height Checker
    • 1380. Lucky Numbers in a Matrix
  • C#
    • 1. Two Sum
    • 7. Reverse Integer
    • 9. Palindrome Number
    • 13. Roman to Integer
    • 14. Longest Common Prefix
    • 20. Valid Parentheses
    • 21. Merge Two Sorted Lists
    • 26. Remove Duplicates from Sorted Array
    • 27. Remove Element
    • 28. Implement strStr()
    • 35. Search Insert Position
    • 38. Count and Say
    • 53. Maximum Subarray
    • 58. Length of Last Word
    • 191. Number of 1 Bits
    • 231. Power of Two
    • 401. Binary Watch
    • 1114. Print in Order
Powered by GitBook
On this page

Was this helpful?

  1. Ruby

26. Remove Duplicates from Sorted Array

Previous21. Merge Two Sorted ListsNext27. Remove Element

Last updated 4 years ago

Was this helpful?

Given a sorted array nums, remove the duplicates such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
  return 0 if nums.empty?
  nums.each_with_index do |v,index|
    if nums[index+1] == v
      nums[index] = nil
    end
  end
  return nums.delete(nil)
end
in-place
in-place