Earliest Non-Repeating Event Name in a Sequence
Arrays & HashingAuto-graded · 18 test casesLast updated
Find the earliest entry in a list of strings that occurs exactly once overall, or return an empty string, testing frequency counting combined with preserved order.
Earliest Non-Repeating Event Name
A monitoring system records a sequence of event names, in the order they occurred. Some names show up many times, others only once.
Write a function that returns the event name that occurs exactly once in the whole sequence and whose position is the smallest among all such names. If no name occurs exactly once, return an empty string "".
Details
- Names are compared by exact string equality, so
"Login"and"login"are different names. - The count of a name is taken over the entire list, not just the part before it. A name that looks new at its first position but appears again later does not qualify.
- If every name is unique, the answer is the first element.
- If the list is empty, return
"".
Constraints
0 <= len(logs) <= 10^5(the list is normally non-empty, but an empty list must be handled by returning"")- Each string has length between 1 and 50.
- Each string consists of alphanumeric characters (underscores may also appear in the samples).
Examples
Example 1
Input: first_single_event(['login', 'request_data', 'login', 'process_data', 'request_data'])
Output: 'process_data'
Example 2
Input: first_single_event(['error', 'error', 'warning', 'warning'])
Output: ''
Hints
Hint 1
Knowing how often each name occurs is not enough by itself, because counts do not tell you which name came first. Think about what extra information you need.
Hint 2
Count all occurrences first using a hash map. Only after the counts are final can you tell which names are truly unique.
Hint 3
Make a second pass over the original list from left to right and return the first entry whose count is 1. Return an empty string if the pass ends without a result.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two questions are involved: how many times does each name occur, and which of the single-occurrence names comes first?
- First pass: build a frequency map from name to count, for example with
Counter. - Second pass: walk through the original list from left to right and return the first name whose count equals 1.
- If the second pass finishes without returning, no name is unique, so return
"".
Iterating the original list (rather than the map) gives the ordering directly and does not depend on any map ordering behavior.
Why it is correct
- After the first pass, each count is the exact total number of occurrences in the whole list. A name that appears unique early but repeats later therefore has a count above 1 and is never returned.
- The second pass visits indices in increasing order, so the first index whose name has count 1 is the earliest name that occurs exactly once.
- If no name has count 1, the loop ends and the empty string is returned. An empty input also falls through to this case.
Complexity
- Time: O(N * L), where N is the number of entries and L (at most 50) is the string length, since hashing a string costs O(L). Treating L as a constant gives O(N).
- Space: O(U * L), where U is the number of distinct names (at most N).
Reference solution
from collections import Counter
from typing import List
def first_single_event(logs: List[str]) -> str:
counts = Counter(logs)
for log in logs:
if counts[log] == 1:
return log
return ""