We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b52fa15 commit fc75194Copy full SHA for fc75194
1 file changed
Offer/超过一半是数字.java
@@ -0,0 +1,42 @@
1
+package Offer;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
6
+public class 超过一半是数字
7
+{
8
+ public static void main(String[] args)
9
+ {
10
+ int[] a = {1, 2, 3, 2, 2, 2, 5, 4, 2};
11
+ int re = fun(a);
12
+ System.out.println(re);
13
+ }
14
15
+ private static int fun(int[] a)
16
17
+ Map<Integer, Integer> map = new HashMap<Integer, Integer>();
18
19
+ for (int i = 0; i < a.length; i++)
20
21
+ int temp;
22
+ if (map.get(a[i]) == null)
23
24
+ map.put(a[i], 1);
25
+ temp = 1;
26
27
+ else
28
29
+ temp = map.get(a[i]);
30
+ map.put(a[i], ++temp);
31
32
+ if (temp > a.length / 2)
33
34
+ System.out.println(a.length + " " + temp);
35
+ return a[i];
36
37
38
39
+ return 0;
40
41
42
+}
0 commit comments