📘
snesjhon
  • You Should Know
  • Personal
    • Blogs
      • I'll never complain about web tooling again.
      • vscodevim
  • JavaScript
    • Closures
      • Closure Q1
    • Values
      • Primitive vs Reference
      • Accessing by value and reference
    • Function
      • call, apply, bind
      • Pass by value
      • Different types of Scopes
      • Context vs Scope
      • Parse Time Vs Run Time
    • Hosting
      • Are Let & Const Hoisted?
      • Hoisting Q1
      • Hoisting Q2
    • Standard Objects
      • Math
        • Math.log10
        • Math.pow()
    • Array
      • Apply
      • Slice vs Splice vs Split
    • This
      • This - Q1
    • TypeScript
      • as const
    • FAQs
      • Modulo Operator
      • Timeout
      • Declarative vs Imperative
      • ++i vs. i++
    • Interview Questions
  • react
    • FAQs
  • Ruby
    • Debugging
    • Symbols
    • Intro
  • Algorithms
    • Sliding Window
      • minSubArrayLen
      • maxSubArraySum
      • findLongestSubstring
    • Frequency Counter
      • sameFrequency
    • Recursion
      • nestedEvenSum
      • flatten
      • Reverse a String
      • Fibonnacci
    • Searching
      • overlappingIntervals
      • twoStringSearch
      • binarySearch
    • Sort - Elementary
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • quickSort
    • Sort - Intermediate
      • Radix Sort
      • Merge Sort
    • FAQs
  • Data Structures
    • Breadth First Search
    • Linked Lists
      • Singly Linked Lists
    • FAQs
  • Code Challenges
    • LeetCode
      • removeDuplicates
    • Hacker Rank
      • twoSums
      • Sock Merchant
    • Hacker Rank - Medium
      • New Year Chaos
  • Databases
    • SQL
Powered by GitBook
On this page
  1. Code Challenges
  2. LeetCode

removeDuplicates

Last updated 4 years ago

Given a sorted array nums, remove the duplicates in-place 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 in-place with O(1) extra memory.

Input: [1,1,2]
Output: 2

Input: [0,0,1,1,1,2,2,3,3,4]
Output: 5
function removeDuplicates(arr) {
  // We want to start at 1 because the first element of the array will ALWAYS be unique no matter t
  // the length of the array.
  let index = 1;

  // We then go through the array and figure out the input
  for (let i = 0; i < arr.length - 1; i++) {
    if (arr[i] !== arr[i + 1]) {
      arr[index++] = arr[i + 1];
    }
  }

  return index;
}

// Although we're only returning the length of the array, since the original
// array is passed by reference, the original array would be modified.
let nums = [1, 1, 2];
let len = removeDuplicates(nums);
for (let i = 0; i < len; i++) {
  // nums === [1, 2, 2] -> this is because we've overwriten the array through reference
  console.log(nums[i]); // 1,2
}

removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]);

Resources

https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/727/