-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityelement.java
More file actions
30 lines (26 loc) · 885 Bytes
/
majorityelement.java
File metadata and controls
30 lines (26 loc) · 885 Bytes
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
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.lang.Number;
class majorityelement {
public static void main(String[] args) {
// put your code here
Scanner in = new Scanner(new InputStreamReader(System.in));
Integer n = new Integer(in.nextInt());
Integer nhalf = new Integer(n/2);
HashMap<Long,Integer> myMap = new HashMap<Long,Integer>(n);
Long cur;
Integer count = new Integer(0);
for( int i = 0; i < n; i++ ) {
cur = new Long(in.nextLong());
if(myMap.containsKey(cur) == false)
myMap.put( cur, count );
count = myMap.get(cur);
myMap.put( cur, new Integer(count.intValue() + 1) );
if( new Integer(count.intValue() + 1) > nhalf)
System.out.println(1);
count = new Integer(0);
}
System.out.println(0);
}
}