-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMode.java
More file actions
57 lines (42 loc) · 1.35 KB
/
Copy pathArrayMode.java
File metadata and controls
57 lines (42 loc) · 1.35 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
46
47
48
49
50
51
52
53
54
55
56
57
package com.qb.israel.codefights;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ArrayMode {
public static void main(String[] args) {
ArrayMode am = new ArrayMode();
am.arrayMode(new int []{1,2,2,1,3,1,4,4,4,4,1,2,2,2,2,3,4,5,5,6});
}
public int arrayMode(int [] sequence)
{
Set<Integer> numbers = new HashSet<Integer>();
Map<Integer,Integer> mapNumber = new HashMap<Integer,Integer>();
for(int i = 0; i < sequence.length; i++) {
numbers.add(sequence[i]);
}
for (Integer x : numbers) {
mapNumber.put(x, 0);
}
for(int i = 0; i < sequence.length; i++) {
if (mapNumber.containsKey(sequence[i])) {
mapNumber.put(sequence[i],mapNumber.get(sequence[i])+1);
}
}
int max = Collections.max(mapNumber.values());
for (Entry<Integer, Integer> entry : mapNumber.entrySet()) { // Itrate through hashmap
if (entry.getValue()==max) {
System.out.println(entry.getKey());
return entry.getKey(); // Print the key with max value
}
}
return 0;
}
public int arrayModeImproved()
{
int [] numbers = new int []{1,2,2,1,3,1,4,4,4,4,1,2,2,2,2,3,4,5,5,6};
return 0;
}
}