Question
Remove Prefix to Make All Elements Distinct
Faizan is given a list of integers a of length n.  A list is considered good if it contains only distinct elements. To make the list good, Faizan can perform the following operation any number of times (possibly zero):
  • Remove the first (leftmost) element of the list.
Your task is to determine the minimum number of elements that must be removed from the beginning of the list so that all remaining elements are distinct. In other words, find the length of the smallest prefix of the list which, when removed, leaves a list with all unique values.
Input
  • The first line contains an integer n — the length of the sequence.
  • The second line contains n space-separated integers a₁, a₂, …, aₙ.
Output
Print a single integer — the minimum number of elements that must be removed from the beginning so that all remaining elements are distinct.
Example
Input 1:
Input
4
3 1 4 3

Output
1

Explanation:
After removing the shortest valid prefix , the remaining list is:
[1, 4, 3]
Remaining list contain only distinct elements, and the number of removed elements is minimized.


Input 2:
Input
6
6 1 2 3 2 9

Output
3

Explanation:
After removing the shortest valid prefix , the remaining list is:
[3, 2, 9]
Remaining list contain only distinct elements, and the number of removed elements is minimized.

Online