Find the First Unique Element
MediumPractice library · +20 XPhash maparraysfrequency
Given an array of N integers, find the first integer that appears exactly once. If no such integer exists, return -1. The order of elements in the input array matters.
Input
The first line contains an integer N. The second line contains N space-separated integers.
Output
Print the first integer that appears exactly once, or -1 if none exists.
Constraints
1 <= N <= 200000. Each integer will be between -10^9 and 10^9.
Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests
Example 1
Input
5 1 2 3 2 1
Output
3
3 is the first element that appears only once.
Example 2
Input
3 5 5 5
Output
-1
No element appears exactly once, so output -1.
Hints
Similar problems
- Longest Subarray with At Most K Duplicate PairsMedium
- Distinct Consecutive PairsMedium
- Distinct Elements in a Sliding WindowMedium
- Count Matching PairsMedium
- Subarray with Target ProductMedium
- Count Elements with Equal NeighborsMedium
More topics: Arrays problems, Hash Map and Hashing problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 3 2 1
Example 2 input
3 5 5 5