Problems

Find Duplicate

HardArrays

Given an array of integers, where each integer is in the range [1, n], write a program to find a duplicate in the array.

Input:

  • The first line contains the number of elements in the array.
  • The second line contains the elements of the array separated by spaces.

Output:

  • A duplicate in the array.

Example 1

Input: 5 1 3 4 2 2

Output: 2

The duplicate in the array is 2.

Constraints

  • 1 <= n <= 10^5
  • 1 <= a[i] <= n

Hints

arraysduplicate
Find Duplicate | CodeForge AI