Duplicate Requisitions
Stardate 55122.8 ยท UES Magnanimous
Briefing
Ensign Tannenbaum, worried that his requisition for a replacement plasma coupling "might not have gone through", submitted it eleven times. It went through eleven times. Stores now has a list of forty items with a great many repeats and a standing question about whether the Ensign is allowed near forms.
Chief T'Kala wants the list with each item appearing once, keeping the first occurrence in place, so the order stays the order things were asked for. She has been clear that sorting it would be cheating, and that she can tell.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขRepeats removed, first appearance kept in place
- โขAn already-unique list is unchanged
- โขEvery item the same collapses to one
- โขOrder is preserved, not sorted
- โขAn empty list stays empty
Objective
Write unique_in_order(items): return a new list with duplicates removed,
keeping the first occurrence of each item and the original order of first appearances.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write unique_in_order(items: &[&str]) -> Vec<String>:
duplicates removed, keeping the first occurrence of each item and the original order of
first appearances.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Walk the list keeping a record of what you have already seen (a set is ideal), and only keep an item the first time. Converting to a set and back loses the order, which is the fourth objective.
Debrief
Twelve distinct items, in the order they were first requested. Ensign Tannenbaum has been issued one plasma coupling and a short document titled "Once Is Enough", which he has read, he says, several times.