-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMajority.java
More file actions
40 lines (39 loc) · 1.11 KB
/
ArrayMajority.java
File metadata and controls
40 lines (39 loc) · 1.11 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
import java.util.Scanner;
public class ArrayMajority {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int flag=0, index=0;
System.out.print("Enter a size of Array :- ");
int n=obj.nextInt();
int array[]=new int[n];
System.out.println("Enter a element in array :- ");
for(int i=0;i<n;i++){
array[i]=obj.nextInt();
}
int max=array[0];
for(int i=1;i<n;i++){
if(array[i]>max){
max=array[i];
}
}
System.out.println("maximun :- "+max);
int temp[]=new int[max+1];
for(int i=0;i<n;i++){
temp[array[i]]+=1;
// System.out.println("index :- "+temp[i]);
}
for(int i=0;i<=max;i++){
if(temp[i]>(n/2)){
flag=1;
index=i;
}
}
if(flag==1){
System.out.println("yes :- "+index);
}
else{
System.out.println("no !");
}
obj.close();
}
}