-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindTheDifferenceOfTwoArrays.java
More file actions
60 lines (46 loc) · 1.77 KB
/
FindTheDifferenceOfTwoArrays.java
File metadata and controls
60 lines (46 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package Algorithms.Hashing;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 15 April 2025
*/
public class FindTheDifferenceOfTwoArrays {
public static void main(String[] args) {
int[] nums1 = {1, 2, 3};
int[] nums2 = {2, 4, 6};
System.out.println("findDifference(nums1, nums2) => " + findDifference(nums1, nums2));
}
public static List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(new ArrayList<Integer>());
result.add(new ArrayList<Integer>());
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
for (int x: nums1) set1.add(x);
for (int x: nums2) set2.add(x);
for (int x: set1) if (!set2.contains(x)) result.get(0).add(x);
for (int x: set2) if (!set1.contains(x)) result.get(1).add(x);
return result; // or Arrays.asList(list1, list2);
}
public List<List<Integer>> findDifferenceMyApproach(int[] nums1, int[] nums2) {
List<List<Integer>> lst = new ArrayList<>();
Set<Integer> set = new HashSet<>();
Set<Integer> subLst = new HashSet<>(); // or use List, just by trav "set" instead of nums1, nums2
for(int x: nums2) set.add(x);
for(int x:nums1) {
if(!set.contains(x)) subLst.add(x);
}
lst.add(new ArrayList<>(subLst));
subLst = new HashSet<>();
set.clear();
for(int x: nums1) set.add(x);
for(int x:nums2) {
if(!set.contains(x)) subLst.add(x);
}
lst.add(new ArrayList<>(subLst));
return lst;
}
}