
-Code
import java.util.*;
class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
// 저장할 리스트
List<Integer> numsList1 = new ArrayList<>();
List<Integer> numsList2 = new ArrayList<>();
// 셋으로 두 배열을 저장
Set<Integer> numsSet1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet());
Set<Integer> numsSet2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet());
// 다른 셋 리스트에 값이 있는지 확인
for (int num : numsSet1) {
if (!numsSet2.contains(num)) {
numsList1.add(num);
}
}
for (int num : numsSet2) {
if (!numsSet1.contains(num)) {
numsList2.add(num);
}
}
List<List<Integer>> answer = new ArrayList<>();
answer.add(numsList1);
answer.add(numsList2);
return answer;
}
}
stream을 사용해서 풀어보았습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [LeetCode/Java] Number of Recent Calls (0) | 2025.12.31 |
|---|---|
| [LeetCode/Java] Unique Number of Occurrences (0) | 2025.12.31 |
| [LeetCode/Java] Find Pivot Index (0) | 2025.12.31 |
| [LeetCode/Java] Find the Highest Altitude (0) | 2025.12.31 |
| [LeetCode/Java] Maximum Average Subarray I (0) | 2025.12.31 |