U

13. Detect Duplicate Elements in Array

Easy

Problem Description

Given an array of NN integers, determine whether any element occurs more than once.

Input Format

  • •Line 1: An integer NN (1≤N≤1051 \le N \le 10^5).
  • •Line 2: NN space-separated integers A1,A2,…,ANA_1, A_2, \dots, A_N.

Output Format

Print true if at least one number appears more than once, or false if all elements are distinct.

Example 1:
Input:
5
1 2 3 4 2
Output:
true
Explanation: The number 2 appears twice.
Example 2:
Input:
4
1 2 3 4
Output:
false
Explanation: All elements are distinct.
Example 3:
Input:
1
99
Output:
false
Explanation: An array of size 1 cannot contain duplicates.
Constraints:
  • 1 <= N <= 10^5
  • -10^9 <= A[i] <= 10^9
  • Time complexity target: O(N log N) or O(N)
  • Auxiliary space target: O(N) or O(1)
Follow up:How would you detect duplicates in O(N) time and O(1) extra space if numbers are constrained in [1, N]?
1,180 Online