I can't see where is the problem, it prints the most common bytes from mine test file.
package pl.codegym.task.task18.task1803;
import java.io.*;
import java.util.*;
/*
Najczęściej występujące bajty
*/
public class Solution {
public static void main(String[] args) throws Exception {
printMostCommonBytes(createMapFromListOfBytesAndCount(createListFromInputStreamAndSortIt(createFile(getFileName()))));
}
public static String getFileName() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
}
public static FileInputStream createFile(String fileName) throws FileNotFoundException {
return new FileInputStream(fileName);
}
public static List<Integer> createListFromInputStreamAndSortIt(FileInputStream inputStream) throws IOException {
List<Integer> list = new ArrayList<>();
while (inputStream.available() > 0) {
list.add(inputStream.read());
}
list.sort(Comparator.naturalOrder());
inputStream.close();
return list;
}
public static Map<Integer, Integer> createMapFromListOfBytesAndCount(List<Integer> list) {
Map<Integer, Integer> map = new HashMap<>();
int count = 1;
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i).equals(list.get(i + 1))) {
count++;
} else {
map.put(list.get(i), count);
count = 1;
}
}
return map;
}
public static void printMostCommonBytes(Map<Integer, Integer> map) {
int max = map.entrySet().iterator().next().getValue();
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if(entry.getValue() > max) {
max = entry.getValue();
}
}
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if(entry.getValue() == max) {
System.out.print(entry.getKey() + " ");
}
}
}
}