Skip to main content

Command Palette

Search for a command to run...

Day-2 Leetcode Interview Questions

Published
•3 min read•View as Markdown

26. Remove Duplicates from Sorted Array — LeetCode (Easy)

Problem Summary

You are given an integer array nums that is sorted in non-decreasing order.
Your task is to remove duplicates in-place so that each unique value appears exactly once.

Key requirements:

  • The relative order must remain the same.

  • After removal, the first k elements of nums should contain the unique values.

  • The remaining elements beyond index k - 1 can be ignored.

  • You must return k, the number of unique elements.

This is an in-place algorithm, meaning no extra array should be used.

Intuition / Approach

Since the array is already sorted, all duplicates appear next to each other.
This makes it easy to skip repeated values.

A simple way to solve this is using the two-pointer technique:

Idea

  • Maintain a pointer count — this tracks where the next unique element should be stored.

  • Iterate through the array:

    • If the current element is not equal to the next one, it means this is the last occurrence of that value.

    • Copy it into nums[count], then increment count.

This ensures:

  • All unique elements are compacted towards the front.

  • The order remains sorted.

  • No extra memory is used.

Java Solution

class Solution {
    public int removeDuplicates(int[] nums) {
        int count = 0;

        for (int i = 0; i < nums.length; i++) {
            if (i < nums.length - 1 && nums[i] == nums[i + 1]) {
                continue;
            } else {
                nums[count] = nums[i];
                count++;
            }
        }

        return count;
    }
}

Time & Space Complexity

ComponentComplexity
TimeO(n) — each element is visited once
SpaceO(1) — in-place, no extra array used

80. Remove Duplicates from Sorted Array II — LeetCode (Medium)

Problem Summary

You are given an integer array nums, sorted in non-decreasing order.
Your task is to remove duplicates in-place, but with one rule:

Each unique number may appear at most twice.

After removing extra duplicates:

  • The first k elements of nums should contain the valid result.

  • The elements beyond index k - 1 can be ignored.

  • You must return k, the count of elements that remain after filtering.

  • You cannot use extra space — the solution must be O(1) space and in-place.

Intuition / Approach

This problem is similar to the basic “Remove Duplicates from Sorted Array,” but here each number can appear at most twice. Since the array is sorted, duplicates appear together, which makes it easy to control how many times each value is written.

We use a two-pointer technique:

  • i → write pointer (tracks where the next valid number should go)

  • Loop through each number n in nums:

    • If i < 2, we always accept n (because we can have at least two of any number).

    • Otherwise, we compare n with the element at nums[i - 2].

      • If they are different, it means this number hasn't appeared twice yet → write it.

      • If they are same, it means we already have two copies → skip it.

The key condition:

n != nums[i - 2]

ensures that no number appears more than twice. The result stays sorted, done fully in-place, and uses O(1) extra memory.

Java Solution

class Solution {
    public int removeDuplicates(int[] nums) {
        int i = 0;
        for (int n : nums) {
            if (i < 2 || n != nums[i - 2]) {
                nums[i++] = n;
            }
        }
        return i;
    }
}

Time & Space Complexity

ComponentComplexity
TimeO(n) — each element is processed once
SpaceO(1) — no extra array, in-place