Longest-Waiting Unique Task
Problem statement
The array tasks records task sightings in chronological order. A task is still waiting only when its identifier appears exactly once in the complete array.
Among all waiting tasks, return the identifier whose only sighting occurs earliest. This is the task that has waited the longest. If no task is waiting, return the empty string.
Task identifiers are compared case-sensitively.
Sibling variants: same core, different skin
These four problems are sibling variants of the same first-unique pattern. Their story wrappers and query timing differ, while the core rule keeps the earliest value whose frequency is exactly one.
- Longest-Waiting Unique Task (current)
- Dynamic Longest-Waiting Unique Task Queries
- First User to Log In Exactly Once
- First Unique Character in a Stream
Function
longestWaitingUniqueTask(tasks: String[]) → StringExamples
Example 1
tasks = ["A","B","C","D","A"]return = "B"A appears twice, so it is not waiting. B, C, and D each appear once, and B occurs earliest.
Example 2
tasks = ["A","B","A","B"]return = ""Both identifiers appear more than once, so no task remains waiting.
Example 3
tasks = ["build","deploy","test"]return = "build"Every task appears once, so the first task has waited the longest.
Constraints
1 <= tasks.length <= 100000.- Each task identifier is a nonempty alphanumeric string of length at most
40. - Task identifiers are case-sensitive.