Merged Departure Board

Easytwo pointersAlso known as Remove Duplicates from Sorted Array

A small station's departure screen merges two feeds, trains and buses, into one list sorted by time. Times are minutes after midnight, so 6:05 is 365.

Departures that share a time show as rows like 365, 365, but the screen has room for one row per time. Its tiny controller cannot afford a second list, so it must clean up the one it has.

Your task

Write dedupeBoard(times). times is sorted from earliest to latest. Change it in place so it holds each distinct time exactly once, still sorted, and nothing else. Return nothing.

Do not build a second list. Overwrite values inside times, then cut off the leftover tail (in JavaScript set times.length; in Python use del times[k:], where k is how many times you kept).

dedupeBoard(times: int[]) → void (changes times in place)

Examples

Example 1
Input
times = [365, 365, 410, 455, 455, 455, 520]
Output
[365, 410, 455, 520]
Why

365 appears twice and 455 three times. Keep one of each: 365, 410, 455, 520. The list shrinks from 7 entries to 4.

Example 2
Input
times = [480, 495, 510]
Output
[480, 495, 510]
Why

No time repeats, so the list is unchanged.

Constraints

  • 0 ≤ times.length ≤ 10,000
  • 0 ≤ times[i] ≤ 1,439
  • times is sorted in non-decreasing order.
Start with brute force

Walk the list and delete each entry equal to the one before it (splice in JavaScript, del or pop(i) in Python).

Every delete shifts all later entries left. 10,000 entries that are almost all repeats means close to 10,000 deletes, about 50 million moves. Writing each kept entry once, straight to its final spot, avoids the shifting.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code