Hash set · Easy
An API audit stream contains request IDs in arrival order. Operations wants the first event that repeats an earlier ID.
Return the index of the earliest duplicate occurrence.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ ids:string[] }
Input rules
- IDs are case-sensitive and may be empty.
What each field means
ids— request IDs in arrival order. IDs are case-sensitive, and empty IDs are valid.
Output
Return value and rules
- Return the zero-based index of the first arrival whose ID appeared earlier.
- Return the duplicate occurrence’s index, not the original occurrence’s index.
- Return
-1if every arrival has a new ID.
Constraints
- 0 <= ids.length <= 100000
- Each ID contains at most 80 ASCII characters.
Examples
Example 1
{
"ids": [
"a",
"b",
"b",
"a"
]
}Returns
2Example 2
{
"ids": []
}Returns
-1Required complexity
O(n) time and O(n) space