diff --git a/DIRECTORY.md b/DIRECTORY.md index b880021a6ddb..1c1c010221a3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -858,6 +858,7 @@ * [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) * [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) * [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) diff --git a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Intersection.java b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Intersection.java index 54bd10de50fa..0e49218d6348 100644 --- a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Intersection.java +++ b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Intersection.java @@ -1,27 +1,57 @@ 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.Collections; import java.util.HashMap; import java.util.List; 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. + *
+ * 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. + *
+ * + *+ * Example: + *
+ * int[] array1 = {1, 2, 2, 1}; + * int[] array2 = {2, 2}; + * List+ * + * + *result = Intersection.intersection(array1, array2); // result will contain [2, 2] + *
+ * Note: The order of the returned list may vary since it depends on the order of elements + * in the input arrays. + *
+ */ 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