Skip to content

Commit 7d64c59

Browse files
authored
[BAEL-7993] Count the number of occurrences in an array in Java (#16714)
* feat: count occurences in array * fix: review * fix: review
1 parent 528604a commit 7d64c59

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baledung.algorithms.countoccurence;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.function.Function;
7+
import java.util.stream.Collectors;
8+
9+
public class CountOccurrence {
10+
11+
public static int[] countOccurrencesWithCounter(int[] elements, int n) {
12+
int[] counter = new int[n];
13+
14+
for (int element : elements) {
15+
counter[element]++;
16+
}
17+
18+
return counter;
19+
}
20+
21+
public static <T> Map<T, Integer> countOccurrencesWithMap(T[] elements) {
22+
23+
Map<T, Integer> counter = new HashMap<>();
24+
25+
for (T element : elements) {
26+
counter.merge(element, 1, Integer::sum);
27+
}
28+
29+
return counter;
30+
}
31+
32+
public static <T> Map<T, Long> countOccurrencesWithStream(T[] elements) {
33+
34+
return Arrays.stream(elements)
35+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.algorithms.countoccurence;
2+
3+
import static com.baledung.algorithms.countoccurence.CountOccurrence.countOccurrencesWithCounter;
4+
import static com.baledung.algorithms.countoccurence.CountOccurrence.countOccurrencesWithMap;
5+
import static com.baledung.algorithms.countoccurence.CountOccurrence.countOccurrencesWithStream;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
import java.util.Map;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
public class CountOccurrenceUnitTest {
13+
14+
@Test
15+
void givenArrayOfIntegers_whenCountOccurrenceWithCounter_thenCounterIndexIsOccurrence() {
16+
int[] counter = countOccurrencesWithCounter(new int[] { 2, 3, 1, 1, 3, 4, 5, 6, 7, 8 }, 10);
17+
assertEquals(2, counter[3]);
18+
}
19+
20+
@Test
21+
void givenArrayOfIntegers_whenCountOccurrenceWithMap_thenMapValueIsOccurrence() {
22+
Map<Integer, Integer> counter = countOccurrencesWithMap(new Integer[] { 2, 3, 1, -1, 3, 4, 5, 6, 7, 8, -1 });
23+
assertEquals(2, counter.get(-1));
24+
}
25+
26+
@Test
27+
void givenArrayOfString_whenCountOccurrenceWithMap_thenMapValueIsOccurrence() {
28+
Map<String, Integer> counter = countOccurrencesWithMap(new String[] { "apple", "orange", "banana", "apple" });
29+
assertEquals(2, counter.get("apple"));
30+
}
31+
32+
@Test
33+
void givenArrayOfString_whenCountOccurrenceWithStream_thenMapValueIsOccurrence() {
34+
Map<String, Long> counter = countOccurrencesWithStream(new String[] { "apple", "orange", "banana", "apple" });
35+
assertEquals(2, counter.get("apple"));
36+
}
37+
38+
@Test
39+
void givenArrayOfIntegers_whenCountOccurrenceWithStream_thenMapValueIsOccurrence() {
40+
Map<Integer, Long> counter = countOccurrencesWithStream(new Integer[] { 2, 3, 1, -1, 3, 4, 5, 6, 7, 8, -1 });
41+
assertEquals(2, counter.get(-1));
42+
}
43+
}

algorithms-modules/algorithms-miscellaneous-9/src/test/java/com/baeldung/algorithms/permutation/StringPermutationUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.baeldung.algorithms.permutation;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import static com.baeldung.algorithms.permutation.StringPermutation.isPermutationInclusion;
64
import static com.baeldung.algorithms.permutation.StringPermutation.isPermutationWithMap;
75
import static com.baeldung.algorithms.permutation.StringPermutation.isPermutationWithOneCounter;
@@ -10,6 +8,8 @@
108
import static org.junit.jupiter.api.Assertions.assertFalse;
119
import static org.junit.jupiter.api.Assertions.assertTrue;
1210

11+
import org.junit.jupiter.api.Test;
12+
1313
public class StringPermutationUnitTest {
1414

1515
@Test

0 commit comments

Comments
 (0)