Given an array of integers, write a function that determines whether the array contains any duplicates. Your function should return true if any element appears at least twice in the array, and it should return false if every element is distinct.
Example
For a = [1, 2, 3, 1], the output should be
solution(a) = true.
There are two **1**s in the given array.
For a = [3, 1], the output should be
solution(a) = false.
The given array contains no duplicates.
Input/Output
[execution time limit] 3 seconds (java)
[memory limit] 1 GB
[input] array.integer a
Guaranteed constraints:
0 ≤ a.length ≤ 105,
2 · 109 ≤ a[i] ≤ 2 · 109.[output] boolean
boolean solution(int[] a) {
HashMap <Integer, Integer> map = new HashMap<>();
for (int i : a) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (int i : map.keySet()) {
if (map.get(i) > 1) return true;
}
return false;
}