-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhoHasMajorityArray.java
More file actions
49 lines (43 loc) · 1.13 KB
/
whoHasMajorityArray.java
File metadata and controls
49 lines (43 loc) · 1.13 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
/*
Given an array arr[] of size N and two elements x and y, use counter variables to find which element appears most in the array, x or y. If both elements have the same frequency, then return the smaller element.
Note: We need to return the element, not its count
*/
public class whoHasMajorityArray
{
public static int majorityWins(int arr[], int n, int x, int y)
{
int xcount=0;
int ycount=0;
for(int i=0;i<n;i++)
{
if(arr[i]==x)
{
xcount++;
}
else if(arr[i]==y)
{
ycount++;
}
}
if(xcount==ycount)
{
return Math.min(x, y);
}
if(xcount>ycount)
{
return x;
}
else
{
return y;
}
}
public static void main(String[] args)
{
int[] a={1,1,2,2,3,3,4,4,4,4,5};
int n=a.length;
int x=4,y=5;
System.out.println(majorityWins(a, n, x, y));
//TC:O(N)
}
}