mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Enhance docs, add tests in Intersection
(#5976)
This commit is contained in:
@ -858,6 +858,7 @@
|
|||||||
* [GenericHashMapUsingArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayListTest.java)
|
* [GenericHashMapUsingArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayListTest.java)
|
||||||
* [GenericHashMapUsingArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java)
|
* [GenericHashMapUsingArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java)
|
||||||
* [HashMapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapTest.java)
|
* [HashMapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapTest.java)
|
||||||
|
* [IntersectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/IntersectionTest.java)
|
||||||
* [LinearProbingHashMapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/LinearProbingHashMapTest.java)
|
* [LinearProbingHashMapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/LinearProbingHashMapTest.java)
|
||||||
* [MajorityElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElementTest.java)
|
* [MajorityElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElementTest.java)
|
||||||
* [MapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/MapTest.java)
|
* [MapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/MapTest.java)
|
||||||
|
@ -1,27 +1,57 @@
|
|||||||
package com.thealgorithms.datastructures.hashmap.hashing;
|
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||||
|
|
||||||
/*
|
|
||||||
* this is algo which implies common mathematical set theory concept
|
|
||||||
* called intersection in which result is common values of both the sets
|
|
||||||
* here metaphor of sets is HashMap
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@code Intersection} class provides a method to compute the intersection of two integer arrays.
|
||||||
|
* The intersection is defined as the set of common elements present in both arrays.
|
||||||
|
* <p>
|
||||||
|
* This class utilizes a HashMap to efficiently count occurrences of elements in the first array,
|
||||||
|
* allowing for an efficient lookup of common elements in the second array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Example:
|
||||||
|
* <pre>
|
||||||
|
* int[] array1 = {1, 2, 2, 1};
|
||||||
|
* int[] array2 = {2, 2};
|
||||||
|
* List<Integer> result = Intersection.intersection(array1, array2); // result will contain [2, 2]
|
||||||
|
* </pre>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: The order of the returned list may vary since it depends on the order of elements
|
||||||
|
* in the input arrays.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
public final class Intersection {
|
public final class Intersection {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the intersection of two integer arrays.
|
||||||
|
* Steps:
|
||||||
|
* 1. Count the occurrences of each element in the first array using a HashMap.
|
||||||
|
* 2. Iterate over the second array and check if the element is present in the HashMap.
|
||||||
|
* If it is, add it to the result list and decrement the count in the HashMap.
|
||||||
|
* 3. Return the result list containing the intersection of the two arrays.
|
||||||
|
*
|
||||||
|
* @param arr1 the first array of integers
|
||||||
|
* @param arr2 the second array of integers
|
||||||
|
* @return a list containing the intersection of the two arrays, or an empty list if either array is null or empty
|
||||||
|
*/
|
||||||
public static List<Integer> intersection(int[] arr1, int[] arr2) {
|
public static List<Integer> intersection(int[] arr1, int[] arr2) {
|
||||||
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
|
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<Integer, Integer> cnt = new HashMap<>(16);
|
Map<Integer, Integer> cnt = new HashMap<>(16);
|
||||||
for (int v : arr1) {
|
for (int v : arr1) {
|
||||||
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
|
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Integer> res = new ArrayList<>();
|
List<Integer> res = new ArrayList<>();
|
||||||
for (int v : arr2) {
|
for (int v : arr2) {
|
||||||
if (cnt.containsKey(v) && cnt.get(v) > 0) {
|
if (cnt.containsKey(v) && cnt.get(v) > 0) {
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class IntersectionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBasicIntersection() {
|
||||||
|
int[] arr1 = {1, 2, 2, 1};
|
||||||
|
int[] arr2 = {2, 2};
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertEquals(List.of(2, 2), result, "Intersection should return [2, 2]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNoIntersection() {
|
||||||
|
int[] arr1 = {1, 2, 3};
|
||||||
|
int[] arr2 = {4, 5, 6};
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertTrue(result.isEmpty(), "Intersection should be empty for disjoint sets");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEmptyArray() {
|
||||||
|
int[] arr1 = {};
|
||||||
|
int[] arr2 = {1, 2, 3};
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertTrue(result.isEmpty(), "Intersection should be empty when first array is empty");
|
||||||
|
|
||||||
|
result = Intersection.intersection(arr2, arr1);
|
||||||
|
assertTrue(result.isEmpty(), "Intersection should be empty when second array is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNullArray() {
|
||||||
|
int[] arr1 = null;
|
||||||
|
int[] arr2 = {1, 2, 3};
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertTrue(result.isEmpty(), "Intersection should be empty when first array is null");
|
||||||
|
|
||||||
|
result = Intersection.intersection(arr2, arr1);
|
||||||
|
assertTrue(result.isEmpty(), "Intersection should be empty when second array is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMultipleOccurrences() {
|
||||||
|
int[] arr1 = {5, 5, 5, 6};
|
||||||
|
int[] arr2 = {5, 5, 6, 6, 6};
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertEquals(List.of(5, 5, 6), result, "Intersection should return [5, 5, 6]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSameElements() {
|
||||||
|
int[] arr1 = {1, 1, 1};
|
||||||
|
int[] arr2 = {1, 1, 1};
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertEquals(List.of(1, 1, 1), result, "Intersection should return [1, 1, 1] for same elements");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLargeArrays() {
|
||||||
|
int[] arr1 = new int[1000];
|
||||||
|
int[] arr2 = new int[1000];
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
arr1[i] = i;
|
||||||
|
arr2[i] = i;
|
||||||
|
}
|
||||||
|
List<Integer> result = Intersection.intersection(arr1, arr2);
|
||||||
|
assertEquals(1000, result.size(), "Intersection should return all elements for identical large arrays");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user