You are given a sorted array arr[] containing positive integers. Your task is…
2026
You are given a sorted array arr[] containing positive integers. Your task is to remove all duplicate elements from this array so that each element appears only once, keeping the distinct elements in the same order in which they appeared.
Input format:
The first line contains a single integer n — the size of arr[].
The second line contains n space-separated integers, the elements of arr[] in non-decreasing order.
Output format:
Print the distinct elements, in their original order, as space-separated integers on a single line.
Examples :
Input:
5
2 2 2 2 2
Output:
2
Explanation: After removing all the duplicates only one instance of 2 remains, so a single value 2 is printed.
Input:
3
1 2 4
Output:
1 2 4
Explanation: The array contains no duplicates, so all three values are printed unchanged.
Constraints:
1 ≤ arr.size() ≤ 105
1 ≤ arr[i] ≤ 106
Show answer & explanation
Concept
In a sorted array all copies of the same value sit next to each other in one contiguous block. Therefore a value is a duplicate exactly when it equals the value kept immediately before it — a purely local test that needs no searching and no extra bookkeeping.
This makes in-place compaction with two indices possible: a read index scans every position once, while a separate write index marks the slot where the next distinct value belongs. The write index never runs ahead of the read index: k ≤ i holds at the start of every iteration, and k ≤ i + 1 immediately after a value has been written, with k = i exactly while no duplicate has been seen yet — so the slot written to has always been read already, and no element still waiting to be read is ever overwritten. Sortedness is what licenses the adjacent comparison; on an unsorted array the same one-pass test would be invalid.
Application
Read n from the first line, then read the n values of arr[] from the second line.
If n is 0 there is nothing to print. Otherwise the first element can never be a duplicate, so keep arr[0] and start the write index at k = 1.
Move a read index i from 1 to n - 1, visiting every position exactly once.
At each step compare arr[i] with the last value already kept, arr[k - 1].
If arr[i] == arr[k - 1], this element repeats the block currently being scanned: skip it and leave k unchanged.
If arr[i] != arr[k - 1], a new distinct value has started: write arr[k] = arr[i] and increment k.
After the pass the first k slots hold every distinct value in its original order; print those k values on one line, separated by single spaces.
Trace on arr[] = [1, 1, 2, 2, 3] (start: keep arr[0] = 1, k = 1):
|
|
| Action | New |
|---|---|---|---|---|
1 | 1 | 1 | equal → skip | 1 |
2 | 2 | 1 | differs → write | 2 |
3 | 2 | 2 | equal → skip | 2 |
4 | 3 | 2 | differs → write | 3 |
The prefix of length k = 3 is [1, 2, 3], so the line printed is 1 2 3.
Cross-check and cost
arr[] = [2, 2, 2, 2, 2]: after keeping the leading 2, every later element equals arr[k - 1] = 2, so k never moves past 1 and the printed line is 2 — matching the first example.
arr[] = [1, 2, 4]: each element differs from the one kept before it, so k reaches 3 and all three values are printed unchanged — matching the second example.
Cost:
arr[0]is kept in O(1) before the loop, and the loop then visits each of the remainingn - 1positions exactly once, doing O(1) work at each — one comparison againstarr[k - 1]and at most one write. That gives O(n) time and O(1) auxiliary space overall. Withnup to 105 this passes comfortably.Contrast with a hash-set approach: a set also removes duplicates but spends O(n) extra memory and discards the very property — sortedness — that makes the single adjacent comparison sufficient here.