KatInterview

Earliest Non-Repeating Event Name in a Sequence

Arrays & HashingAuto-graded · 18 test cases
EasyAuto-gradedArrays & Hashinghash-mapcountingstrings

Last 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.