Skip to content

Commit 64ec00f

Browse files
authored
Merge branch 'master' into testing/AbsoluteValueTest
2 parents 6d5a57b + dd1a51b commit 64ec00f

File tree

5 files changed

+102
-74
lines changed

5 files changed

+102
-74
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,66 @@
88

99
/**
1010
* 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.
1211
* <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).
1514
* </p>
1615
*
1716
* <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
2024
* int[] array1 = {1, 2, 2, 1};
2125
* 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>
2428
* </p>
2529
*
2630
* <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.
2932
* </p>
3033
*/
3134
public final class Intersection {
3235

36+
private Intersection() {
37+
// Utility class; prevent instantiation
38+
}
39+
3340
/**
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+
*
3544
* 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.
4047
*
4148
* @param arr1 the first array of integers
4249
* @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
4452
*/
4553
public static List<Integer> intersection(int[] arr1, int[] arr2) {
4654
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
4755
return Collections.emptyList();
4856
}
4957

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);
5361
}
5462

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);
6068
}
6169
}
62-
return res;
63-
}
6470

65-
private Intersection() {
71+
return result;
6672
}
6773
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.thealgorithms.datastructures.hashmap.hashing;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45
import java.util.HashMap;
56
import java.util.List;
7+
import java.util.Map;
68

79
/**
810
* This class provides a method to find the majority element(s) in an array of integers.
@@ -18,13 +20,18 @@ private MajorityElement() {
1820
* Returns a list of majority element(s) from the given array of integers.
1921
*
2022
* @param nums an array of integers
21-
* @return a list containing the majority element(s); returns an empty list if none exist
23+
* @return a list containing the majority element(s); returns an empty list if none exist or input is null/empty
2224
*/
2325
public static List<Integer> majority(int[] nums) {
24-
HashMap<Integer, Integer> numToCount = new HashMap<>();
26+
if (nums == null || nums.length == 0) {
27+
return Collections.emptyList();
28+
}
29+
30+
Map<Integer, Integer> numToCount = new HashMap<>();
2531
for (final var num : nums) {
2632
numToCount.merge(num, 1, Integer::sum);
2733
}
34+
2835
List<Integer> majorityElements = new ArrayList<>();
2936
for (final var entry : numToCount.entrySet()) {
3037
if (entry.getValue() >= nums.length / 2) {

src/main/java/com/thealgorithms/maths/Convolution.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,21 @@ public static double[] convolution(double[] a, double[] b) {
2323
double[] convolved = new double[a.length + b.length - 1];
2424

2525
/*
26-
The discrete convolution of two signals A and B is defined as:
27-
28-
A.length
29-
C[i] = Σ (A[k]*B[i-k])
30-
k=0
31-
32-
It's obvious that: 0 <= k <= A.length , 0 <= i <= A.length + B.length - 2 and 0 <= i-k <=
33-
B.length - 1 From the last inequality we get that: i - B.length + 1 <= k <= i and thus we get
34-
the conditions below.
26+
* Discrete convolution formula:
27+
* C[i] = Σ A[k] * B[i - k]
28+
* where k ranges over valid indices so that both A[k] and B[i-k] are in bounds.
3529
*/
30+
3631
for (int i = 0; i < convolved.length; i++) {
37-
convolved[i] = 0;
38-
int k = Math.max(i - b.length + 1, 0);
32+
double sum = 0;
33+
int kStart = Math.max(0, i - b.length + 1);
34+
int kEnd = Math.min(i, a.length - 1);
3935

40-
while (k < i + 1 && k < a.length) {
41-
convolved[i] += a[k] * b[i - k];
42-
k++;
36+
for (int k = kStart; k <= kEnd; k++) {
37+
sum += a[k] * b[i - k];
4338
}
39+
40+
convolved[i] = sum;
4441
}
4542

4643
return convolved;

src/main/java/com/thealgorithms/maths/Mode.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,49 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
68

7-
/*
8-
* Find the mode of an array of numbers
9-
*
10-
* The mode of an array of numbers is the most frequently occurring number in the array,
11-
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
9+
/**
10+
* Utility class to calculate the mode(s) of an array of integers.
11+
* <p>
12+
* The mode of an array is the integer value(s) that occur most frequently.
13+
* If multiple values have the same highest frequency, all such values are returned.
14+
* </p>
1215
*/
1316
public final class Mode {
1417
private Mode() {
1518
}
1619

17-
/*
18-
* Find the mode of an array of integers
20+
/**
21+
* Computes the mode(s) of the specified array of integers.
22+
* <p>
23+
* If the input array is empty, this method returns {@code null}.
24+
* If multiple numbers share the highest frequency, all are returned in the result array.
25+
* </p>
1926
*
20-
* @param numbers array of integers
21-
* @return mode of the array
27+
* @param numbers an array of integers to analyze
28+
* @return an array containing the mode(s) of the input array, or {@code null} if the input is empty
2229
*/
2330
public static int[] mode(final int[] numbers) {
2431
if (numbers.length == 0) {
2532
return null;
2633
}
2734

28-
HashMap<Integer, Integer> count = new HashMap<>();
35+
Map<Integer, Integer> count = new HashMap<>();
2936

3037
for (int num : numbers) {
31-
if (count.containsKey(num)) {
32-
count.put(num, count.get(num) + 1);
33-
} else {
34-
count.put(num, 1);
35-
}
38+
count.put(num, count.getOrDefault(num, 0) + 1);
3639
}
3740

3841
int max = Collections.max(count.values());
39-
ArrayList<Integer> modes = new ArrayList<>();
42+
List<Integer> modes = new ArrayList<>();
4043

4144
for (final var entry : count.entrySet()) {
4245
if (entry.getValue() == max) {
4346
modes.add(entry.getKey());
4447
}
4548
}
46-
return modes.stream().mapToInt(n -> n).toArray();
49+
return modes.stream().mapToInt(Integer::intValue).toArray();
4750
}
4851
}
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
package com.thealgorithms.maths;
22

3-
import org.junit.jupiter.api.Assertions;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
47
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
511

612
public class AverageTest {
713

814
private static final double SMALL_VALUE = 0.00001d;
915

10-
@Test
11-
public void testAverageDouble12() {
12-
double[] numbers = {3d, 6d, 9d, 12d, 15d, 18d, 21d};
13-
Assertions.assertEquals(12d, Average.average(numbers), SMALL_VALUE);
16+
@ParameterizedTest(name = "average({0}) should be approximately {1}")
17+
@MethodSource("provideDoubleArrays")
18+
void testAverageDouble(double[] numbers, double expected) {
19+
assertEquals(expected, Average.average(numbers), SMALL_VALUE);
1420
}
1521

16-
@Test
17-
public void testAverageDouble20() {
18-
double[] numbers = {5d, 10d, 15d, 20d, 25d, 30d, 35d};
19-
Assertions.assertEquals(20d, Average.average(numbers), SMALL_VALUE);
22+
@ParameterizedTest(name = "average({0}) should be {1}")
23+
@MethodSource("provideIntArrays")
24+
void testAverageInt(int[] numbers, long expected) {
25+
assertEquals(expected, Average.average(numbers));
2026
}
2127

2228
@Test
23-
public void testAverageDouble() {
24-
double[] numbers = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d};
25-
Assertions.assertEquals(4.5d, Average.average(numbers), SMALL_VALUE);
29+
void testAverageDoubleThrowsExceptionOnNullOrEmpty() {
30+
assertThrows(IllegalArgumentException.class, () -> Average.average((double[]) null));
31+
assertThrows(IllegalArgumentException.class, () -> Average.average(new double[0]));
2632
}
2733

2834
@Test
29-
public void testAverageInt() {
30-
int[] numbers = {2, 4, 10};
31-
Assertions.assertEquals(5, Average.average(numbers));
35+
void testAverageIntThrowsExceptionOnNullOrEmpty() {
36+
assertThrows(IllegalArgumentException.class, () -> Average.average((int[]) null));
37+
assertThrows(IllegalArgumentException.class, () -> Average.average(new int[0]));
38+
}
39+
40+
private static Stream<Arguments> provideDoubleArrays() {
41+
return Stream.of(Arguments.of(new double[] {3d, 6d, 9d, 12d, 15d, 18d, 21d}, 12d), Arguments.of(new double[] {5d, 10d, 15d, 20d, 25d, 30d, 35d}, 20d), Arguments.of(new double[] {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d}, 4.5d), Arguments.of(new double[] {0d, 0d, 0d}, 0d),
42+
Arguments.of(new double[] {-1d, -2d, -3d}, -2d), Arguments.of(new double[] {1e-10, 1e-10, 1e-10}, 1e-10));
43+
}
44+
45+
private static Stream<Arguments> provideIntArrays() {
46+
return Stream.of(Arguments.of(new int[] {2, 4, 10}, 5L), Arguments.of(new int[] {0, 0, 0}, 0L), Arguments.of(new int[] {-1, -2, -3}, -2L), Arguments.of(new int[] {1, 1, 1, 1, 1}, 1L));
3247
}
3348
}

0 commit comments

Comments
 (0)