LeetCode - Algorithms - 442. Find All Duplicates in an Array

Problem

442. Find All Duplicates in an Array

Java

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public List<Integer> findDuplicates(int[] nums) {
final int N = nums.length;
int[] arr = new int[N + 1];
for (int i = 0; i < nums.length; i++) {
arr[nums[i]]++;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < arr.length; i++) {
if (arr[i] > 1) {
list.add(i);
}
}
return list;
}
}

Submission Detail

  • 28 / 28 test cases passed.
  • Runtime: 6 ms, faster than 50.21% of Java online submissions for Find All Duplicates in an Array.
  • Memory Usage: 64.1 MB, less than 5.07% of Java online submissions for Find All Duplicates in an Array.