-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathmajority.java
More file actions
40 lines (35 loc) · 731 Bytes
/
majority.java
File metadata and controls
40 lines (35 loc) · 731 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
31
32
33
34
35
36
37
38
39
40
package algorithmica;
import java.util.*;
public class majority {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int size=in.nextInt();
int[] arr;
arr=new int[size];
for(int i=0;i<arr.length;i++)
arr[i]=in.nextInt();
Arrays.sort(arr);
int prev=arr[0],count=1,maxcount=1,popular=arr[0];
for(int i=1;i<arr.length;i++)
{
if(arr[i]==prev)
{
count++;
}
else
{
if(count>maxcount)
{
maxcount=count;
popular=arr[i-1];
}
count=1;
prev=arr[i];
}
}
popular=count>maxcount?arr[arr.length-1] : popular;
count=(count>maxcount)?count:maxcount;
System.out.println(popular+" "+count);
}
}