Beyond Big O

Sorted deduplication

Sorted deduplication

Easy

Your task

  • Turn a sorted sequence into a new sequence with each value appearing once.
  • Keep the result in ascending order.
  • Return the sequence itself, not a count, and do not modify the input.

Required API

  • DeduplicateSorted
    func DeduplicateSorted(values []int) []int

Behavior

  • Separate calls must not share state.
  • Aim for O(n) time and O(1) auxiliary space, excluding the returned sequence, using only the standard library.
  • Explain why adjacent comparisons are enough.

Constraints

  • The input is sorted in nondecreasing order and may contain repeated values.
  • It is already valid: 0–1,000 integers between -100,000 and 100,000 inclusive.
  • Return an empty list for empty input.
Notes and example notation

Examples use {"values": [...]}.

Examples

The runner calls your functions directly; you do not need to parse JSON.

Example 1 · empty

Arguments
values
[]
Expected return
[]
Test data
Input
{"values":[]}
Expected output
[]

Example 2 · duplicate runs

Arguments
values
[
  -3,
  -3,
  0,
  0,
  0,
  4,
  4
]
Expected return
[
  -3,
  0,
  4
]
Test data
Input
{"values":[-3,-3,0,0,0,4,4]}
Expected output
[-3,0,4]

Example 3 · already distinct

Arguments
values
[
  -2,
  0,
  5
]
Expected return
[
  -2,
  0,
  5
]
Test data
Input
{"values":[-2,0,5]}
Expected output
[-2,0,5]

Example 4 · all equal

Arguments
values
[
  7,
  7,
  7,
  7
]
Expected return
[
  7
]
Test data
Input
{"values":[7,7,7,7]}
Expected output
[7]
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…