-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathClothes.java
More file actions
45 lines (38 loc) · 1.49 KB
/
Copy pathClothes.java
File metadata and controls
45 lines (38 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package clothes;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class Clothes {
public static void main(String[] args) throws IOException {
ArrayList<Integer> data = new ArrayList<>();
Map<Integer, Integer> people = new TreeMap<>();
FileReader file = new FileReader("input.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()){
StringTokenizer st = new StringTokenizer(sc.nextLine(), " ");
while (st.hasMoreTokens()){
data.add(Integer.valueOf(st.nextToken()));
}
}
data.remove(0);
for(Integer color : data){
int count = 1;
if (people.containsKey(color)){
count = people.get(color);
count++;
}
people.put(color, count);
}
List<Map.Entry<Integer, Integer>> list = people.entrySet().stream() // Сортировка в map по значению
.sorted((v1, v2) -> v1.getValue().compareTo(v2.getValue()))
.collect(Collectors.toList());
int max = list.get(list.size()-1).getValue();
int prev = list.size() > 1 ? list.get(list.size()-2).getValue() : 0;
int result = max == prev ? 0 : list.get(list.size()-1).getKey();
FileWriter out = new FileWriter("output.txt");
out.write(String.valueOf(result));
out.close();
}
}