Merged Departure Board

Easytwo pointersAlso known as Remove Duplicates from Sorted Array

A small station shows its departures on one screen. The times come from two feeds, the train company and the bus company, merged into one list sorted by time. Each time is written as minutes after midnight, so 6:05 is 365.

Some departures share a time, and the screen ends up with rows like 365, 365. The screen only has room for one row per time. The board's 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 in sorted order, and nothing else. The list gets shorter when there are repeats. 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 stays exactly as it was.

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 each time an entry equals the one before it, delete it (splice in JavaScript, del or pop(i) in Python).

It works, but every delete shifts all the later entries one place left. A board of 10,000 entries that are almost all repeats does close to 10,000 deletes, each shifting thousands of entries: about 50 million moves. Deciding where each entry goes once, and writing it there once, 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