|
U
Pro13. Detect Duplicate Elements in Array
Easy
Problem Description
Given an array of N integers, determine whether any element occurs more than once.
Input Format
- •Line 1: An integer N (1≤N≤105).
- •Line 2: N space-separated integers A1,A2,…,AN.
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^9Time 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
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit