-
Notifications
You must be signed in to change notification settings - Fork 0
/
Element_Appearing.java
39 lines (37 loc) · 1.2 KB
/
Element_Appearing.java
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
// Leetcode - Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
public class Element_Appearing{
public static void main(String[] args) {
int[] arr = {1,2,2,6,6,6,6,7,10};
System.out.println(findSpecialInteger(arr));
}
//not optimised solution
public static int findSpecialInteger(int[] arr) {
int record = 0;
int element_position = 0;
int virtual = 0;
int n = arr.length;
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
if(arr[i]==arr[j]&& i!=j){
virtual++;
}
if(record<=virtual){
record = virtual+1;
element_position = i;
}
}virtual = 0;
}
return arr[element_position];
}
//optimised
// public int findSpecialInteger(int[] arr) {
// int n = arr.length;
// int target = n/4;
// for(int i = 0;i<(n-target);i++){
// if(arr[i]==arr[i+target]){
// return arr[i];
// }
// }
// return -1;
// }
}