[BAEL-6371_char_frequence] Find the Most Frequent Characters in a String (#13877)

This commit is contained in:
Kai Yuan 2023-04-24 03:46:39 +02:00 committed by GitHub
parent e94486a78d
commit b00b487b4c
3 changed files with 103 additions and 2 deletions

View File

@ -39,8 +39,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>

View File

@ -0,0 +1,58 @@
package com.baeldung.charfreq;
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CharacterWithHighestFrequency {
public static Character byStream(String input) {
return input.chars()
.mapToObj(x -> (char) x)
.collect(groupingBy(x -> x, counting()))
.entrySet()
.stream()
.max(comparingByValue())
.get()
.getKey();
}
public static Set<Character> byMap(String input) {
Map<Character, Integer> map = new HashMap<>();
for (char c : input.toCharArray()) {
map.compute(c, (character, count) -> count == null ? 1 : ++count);
}
int maxCount = map.values()
.stream()
.mapToInt(Integer::intValue)
.max()
.getAsInt();
return map.keySet()
.stream()
.filter(c -> map.get(c) == maxCount)
.collect(toSet());
}
public static Set<Character> byBucket(String input) {
int[] buckets = new int[128];
int maxCount = 0;
for (char c : input.toCharArray()) {
buckets[c]++;
maxCount = Math.max(buckets[c], maxCount);
}
int finalMaxCount = maxCount;
return IntStream.range(0, 128)
.filter(c -> buckets[c] == finalMaxCount)
.mapToObj(i -> (char) i)
.collect(Collectors.toSet());
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.charfreq;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections;
import java.util.Set;
import org.junit.jupiter.api.Test;
import com.google.common.collect.ImmutableSet;
class CharacterWithHighestFrequencyUnitTest {
private static final String INPUT1 = "aaaaaaaaaa(10) bbbbbbb ccccc dddd eee ff";
private static final Set<Character> EXPECTED1 = Collections.singleton('a');
private static final String INPUT2 = "YYYYYYY(7) bbbbb -------(7) dddd eee kkkkkkk(7) ff";
private static final Set<Character> EXPECTED2 = ImmutableSet.of('Y', '-', 'k');
@Test
void whenGettingSingleCharWithHighestFrequencyByStream_shouldSuccess() {
char result1 = CharacterWithHighestFrequency.byStream(INPUT1);
assertEquals('a', result1);
}
@Test
void whenGettingCharWithHighestFrequencyByMap_shouldSuccess() {
Set<Character> result1 = CharacterWithHighestFrequency.byMap(INPUT1);
assertEquals(EXPECTED1, result1);
Set<Character> result2 = CharacterWithHighestFrequency.byMap(INPUT2);
assertEquals(EXPECTED2, result2);
}
@Test
void whenGettingCharWithHighestFrequencyByBucket_shouldSuccess() {
Set<Character> result1 = CharacterWithHighestFrequency.byBucket(INPUT1);
assertEquals(EXPECTED1, result1);
Set<Character> result2 = CharacterWithHighestFrequency.byBucket(INPUT2);
assertEquals(EXPECTED2, result2);
}
}