package com.codegym.task.task18.task1804;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
/*
Most frequent bytes
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s= reader.readLine();
FileInputStream inputStream = new FileInputStream(s);
ArrayList<Integer> list = new ArrayList<>();
Map<Integer,Integer> map = new HashMap<>();
while(inputStream.available()>0){
int temp = inputStream.read();
list.add(temp);
}
inputStream.close();
int count=0;
for(int i=0;i<list.size();i++){
for(int j=0;j<list.size();j++){
if(list.get(i)==list.get(j)){
count++;
}
}
map.put(list.get(i),count);
count=0;
}
int max=0;
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
if (entry.getValue() < max) {
max = entry.getValue();
System.out.print(entry.getKey() + "\t");
}
}
}
}
Please Help
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Henk
3 June 2019, 11:51
same as with the other one: how can I print it if I don't know what they want ?
Least frequent numbers in the file ?? The bottom 3 ? Bottom 2 ? Which ones ? Just the number in file that occurs the least ? Then there's no space needed, as it's one number I print ?
0
Guadalupe Gagnon
17 May 2019, 15:35
Everything is working fine until 36. max = 0 then at line 38 you are checking if the value is less than max, which will always be false because the lowest possible value is 1.
I suggest making an int min at line 26 and setting it to Integer.MAX_VALUE. Then inside the next loop, the one at line 27, right before you reset count to 0, check if count is less than min and set min to count if that is true. Then you can change lines 37-42 to output all keys who's value's are equal to min.
+6