-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhoHasTheMajority.java
More file actions
69 lines (48 loc) · 1.33 KB
/
WhoHasTheMajority.java
File metadata and controls
69 lines (48 loc) · 1.33 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
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
class WhoHasTheMajority {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enthe the size of array :");
int sizeOfArray=sc.nextInt();
System.out.println("Enter the array :");
int arr[]=new int[sizeOfArray];
for(int i=0;i<sizeOfArray;i++){
int a=sc.nextInt();
arr[i]=a;
}
System.out.println("X :");
int x=sc.nextInt();
System.out.println("Y :");
int y=sc.nextInt();
System.out.println("Output :");
System.out.println(majorityWins(arr,sizeOfArray,x,y));
}
public static int majorityWins(int ar[],int n,int X,int Y){
int count_x=0;
int count_y=0;
for(int i=0;i<n;i++){
if(ar[i]==X){
count_x++;
}
}
for(int i=0;i<n;i++){
if(ar[i]==Y){
count_y++;
}
}
if(count_x>count_y){
return X;
}
if(count_x<count_y){
return Y;
}
else{
if(X<Y){
return X;
}
else{
return Y;
}
}
}
}