Skip to content
Katabench
Try free

Remove Duplicates From Sorted Array

Easy About 10 min Free, no account needed

Deduplicate a sorted array without forgetting that it is sorted.

The brief

Given an integer array nums sorted in ascending order, return a new array containing each distinct value once, in order.

The performance bar

The obvious approach ignores the sort: for each element it scans its growing result list to ask "have I added this already?", O(n2). The hidden test has 200,000 distinct values, the worst case for that scan, so the quadratic version is too slow. The input is already sorted, and you will need to use that fact to run in close to O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return each distinct value once, in ascending order.
    public int[] RemoveDuplicates(int[] nums)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Two pointers
Walk a sorted or symmetric sequence from both ends, or fast and slow, instead of checking every pair.

Related katas