-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay4.java
More file actions
35 lines (33 loc) · 856 Bytes
/
Day4.java
File metadata and controls
35 lines (33 loc) · 856 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
public class Day4{
public static void main(String[] args){
}
class tVf{
public static int MajorityElement(int[] v){
int n = v.length;
int count = 0;
int ele = 0;
for(int i = 0 ; i < n ; i++){
if(count == 0){
count = 1;
ele = v[i];
}
else if(ele == v[i]){
count++;
}
else{
count --;
}
}
int count1 = 0;
for(int i = 0; i < n; i++){
if(v[i] == ele){
count1++;
}
}
if(count1 > (n/2)){
return ele;
}
return -1;
}
}
}