|
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * The {@code Intersection} class provides a method to compute the intersection of two integer arrays.
|
11 |
| - * The intersection is defined as the set of common elements present in both arrays. |
12 | 11 | * <p>
|
13 |
| - * This class utilizes a HashMap to efficiently count occurrences of elements in the first array, |
14 |
| - * allowing for an efficient lookup of common elements in the second array. |
| 12 | + * This intersection includes duplicate values — meaning elements are included in the result |
| 13 | + * as many times as they appear in both arrays (i.e., multiset intersection). |
15 | 14 | * </p>
|
16 | 15 | *
|
17 | 16 | * <p>
|
18 |
| - * Example: |
19 |
| - * <pre> |
| 17 | + * The algorithm uses a {@link java.util.HashMap} to count occurrences of elements in the first array, |
| 18 | + * then iterates through the second array to collect common elements based on these counts. |
| 19 | + * </p> |
| 20 | + * |
| 21 | + * <p> |
| 22 | + * Example usage: |
| 23 | + * <pre>{@code |
20 | 24 | * int[] array1 = {1, 2, 2, 1};
|
21 | 25 | * int[] array2 = {2, 2};
|
22 |
| - * List<Integer> result = Intersection.intersection(array1, array2); // result will contain [2, 2] |
23 |
| - * </pre> |
| 26 | + * List<Integer> result = Intersection.intersection(array1, array2); // result: [2, 2] |
| 27 | + * }</pre> |
24 | 28 | * </p>
|
25 | 29 | *
|
26 | 30 | * <p>
|
27 |
| - * Note: The order of the returned list may vary since it depends on the order of elements |
28 |
| - * in the input arrays. |
| 31 | + * Note: The order of elements in the returned list depends on the order in the second input array. |
29 | 32 | * </p>
|
30 | 33 | */
|
31 | 34 | public final class Intersection {
|
32 | 35 |
|
| 36 | + private Intersection() { |
| 37 | + // Utility class; prevent instantiation |
| 38 | + } |
| 39 | + |
33 | 40 | /**
|
34 |
| - * Computes the intersection of two integer arrays. |
| 41 | + * Computes the intersection of two integer arrays, preserving element frequency. |
| 42 | + * For example, given [1,2,2,3] and [2,2,4], the result will be [2,2]. |
| 43 | + * |
35 | 44 | * Steps:
|
36 |
| - * 1. Count the occurrences of each element in the first array using a HashMap. |
37 |
| - * 2. Iterate over the second array and check if the element is present in the HashMap. |
38 |
| - * If it is, add it to the result list and decrement the count in the HashMap. |
39 |
| - * 3. Return the result list containing the intersection of the two arrays. |
| 45 | + * 1. Count the occurrences of each element in the first array using a map. |
| 46 | + * 2. Iterate over the second array and collect common elements. |
40 | 47 | *
|
41 | 48 | * @param arr1 the first array of integers
|
42 | 49 | * @param arr2 the second array of integers
|
43 |
| - * @return a list containing the intersection of the two arrays, or an empty list if either array is null or empty |
| 50 | + * @return a list containing the intersection of the two arrays (with duplicates), |
| 51 | + * or an empty list if either array is null or empty |
44 | 52 | */
|
45 | 53 | public static List<Integer> intersection(int[] arr1, int[] arr2) {
|
46 | 54 | if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
|
47 | 55 | return Collections.emptyList();
|
48 | 56 | }
|
49 | 57 |
|
50 |
| - Map<Integer, Integer> cnt = new HashMap<>(16); |
51 |
| - for (int v : arr1) { |
52 |
| - cnt.put(v, cnt.getOrDefault(v, 0) + 1); |
| 58 | + Map<Integer, Integer> countMap = new HashMap<>(); |
| 59 | + for (int num : arr1) { |
| 60 | + countMap.put(num, countMap.getOrDefault(num, 0) + 1); |
53 | 61 | }
|
54 | 62 |
|
55 |
| - List<Integer> res = new ArrayList<>(); |
56 |
| - for (int v : arr2) { |
57 |
| - if (cnt.containsKey(v) && cnt.get(v) > 0) { |
58 |
| - res.add(v); |
59 |
| - cnt.put(v, cnt.get(v) - 1); |
| 63 | + List<Integer> result = new ArrayList<>(); |
| 64 | + for (int num : arr2) { |
| 65 | + if (countMap.getOrDefault(num, 0) > 0) { |
| 66 | + result.add(num); |
| 67 | + countMap.computeIfPresent(num, (k, v) -> v - 1); |
60 | 68 | }
|
61 | 69 | }
|
62 |
| - return res; |
63 |
| - } |
64 | 70 |
|
65 |
| - private Intersection() { |
| 71 | + return result; |
66 | 72 | }
|
67 | 73 | }
|
0 commit comments