코딩테스트 풀이/JAVA
[LeetCode/Medium] 442. Find All Duplicates in an Array
무지맘
2023. 2. 21. 22:12
1. Input
1) int[] nums
// nums의 각 요소들은 1개 또는 2개 들어있다.
2. Output
1) nums에 2개씩 들어있는 요소를 담은 List<Integer>
3. Constraint
1) n == nums.length
2) 1 <= n <= 10^5
3) 1 <= nums[i] <= n
4. Example
Input: nums = [4,3,2,7,8,2,3,1] -> Output: [2,3]
5. Code
1) 첫 코드(2023/02/21)
List<Integer> answer = new ArrayList<Integer>();
HashSet s = new HashSet();
for(int i : nums){
if(s.contains(i)) answer.add(i);
else s.add(i);
}
return answer;