Skip to content

Commit f0a5f37

Browse files
committed
refactor: Enhance docs, add tests in Intersection
1 parent 5246f63 commit f0a5f37

File tree

2 files changed

+112
-6
lines changed

2 files changed

+112
-6
lines changed

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Intersection.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
11
package com.thealgorithms.datastructures.hashmap.hashing;
22

3-
/*
4-
* this is algo which implies common mathematical set theory concept
5-
* called intersection in which result is common values of both the sets
6-
* here metaphor of sets is HashMap
7-
*/
8-
93
import java.util.ArrayList;
104
import java.util.Collections;
115
import java.util.HashMap;
126
import java.util.List;
137
import java.util.Map;
148

9+
/**
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+
* <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.
15+
* </p>
16+
*
17+
* <p>
18+
* Example:
19+
* <pre>
20+
* int[] array1 = {1, 2, 2, 1};
21+
* int[] array2 = {2, 2};
22+
* List<Integer> result = Intersection.intersection(array1, array2); // result will contain [2, 2]
23+
* </pre>
24+
* </p>
25+
*
26+
* <p>
27+
* Note: The order of the returned list may vary since it depends on the order of elements
28+
* in the input arrays.
29+
* </p>
30+
*/
1531
public final class Intersection {
1632

33+
/**
34+
* Computes the intersection of two integer arrays.
35+
* 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.
40+
*
41+
* @param arr1 the first array of integers
42+
* @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
44+
*/
1745
public static List<Integer> intersection(int[] arr1, int[] arr2) {
1846
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
1947
return Collections.emptyList();
2048
}
49+
2150
Map<Integer, Integer> cnt = new HashMap<>(16);
2251
for (int v : arr1) {
2352
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
2453
}
54+
2555
List<Integer> res = new ArrayList<>();
2656
for (int v : arr2) {
2757
if (cnt.containsKey(v) && cnt.get(v) > 0) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithms.datastructures.hashmap.hashing;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class IntersectionTest {
10+
11+
@Test
12+
void testBasicIntersection() {
13+
int[] arr1 = {1, 2, 2, 1};
14+
int[] arr2 = {2, 2};
15+
List<Integer> result = Intersection.intersection(arr1, arr2);
16+
assertEquals(List.of(2, 2), result, "Intersection should return [2, 2]");
17+
}
18+
19+
@Test
20+
void testNoIntersection() {
21+
int[] arr1 = {1, 2, 3};
22+
int[] arr2 = {4, 5, 6};
23+
List<Integer> result = Intersection.intersection(arr1, arr2);
24+
assertTrue(result.isEmpty(), "Intersection should be empty for disjoint sets");
25+
}
26+
27+
@Test
28+
void testEmptyArray() {
29+
int[] arr1 = {};
30+
int[] arr2 = {1, 2, 3};
31+
List<Integer> result = Intersection.intersection(arr1, arr2);
32+
assertTrue(result.isEmpty(), "Intersection should be empty when first array is empty");
33+
34+
result = Intersection.intersection(arr2, arr1);
35+
assertTrue(result.isEmpty(), "Intersection should be empty when second array is empty");
36+
}
37+
38+
@Test
39+
void testNullArray() {
40+
int[] arr1 = null;
41+
int[] arr2 = {1, 2, 3};
42+
List<Integer> result = Intersection.intersection(arr1, arr2);
43+
assertTrue(result.isEmpty(), "Intersection should be empty when first array is null");
44+
45+
result = Intersection.intersection(arr2, arr1);
46+
assertTrue(result.isEmpty(), "Intersection should be empty when second array is null");
47+
}
48+
49+
@Test
50+
void testMultipleOccurrences() {
51+
int[] arr1 = {5, 5, 5, 6};
52+
int[] arr2 = {5, 5, 6, 6, 6};
53+
List<Integer> result = Intersection.intersection(arr1, arr2);
54+
assertEquals(List.of(5, 5, 6), result, "Intersection should return [5, 5, 6]");
55+
}
56+
57+
@Test
58+
void testSameElements() {
59+
int[] arr1 = {1, 1, 1};
60+
int[] arr2 = {1, 1, 1};
61+
List<Integer> result = Intersection.intersection(arr1, arr2);
62+
assertEquals(List.of(1, 1, 1), result, "Intersection should return [1, 1, 1] for same elements");
63+
}
64+
65+
@Test
66+
void testLargeArrays() {
67+
int[] arr1 = new int[1000];
68+
int[] arr2 = new int[1000];
69+
for (int i = 0; i < 1000; i++) {
70+
arr1[i] = i;
71+
arr2[i] = i;
72+
}
73+
List<Integer> result = Intersection.intersection(arr1, arr2);
74+
assertEquals(1000, result.size(), "Intersection should return all elements for identical large arrays");
75+
}
76+
}

0 commit comments

Comments
 (0)