-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay 53.2.txt
47 lines (33 loc) · 964 Bytes
/
Day 53.2.txt
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
1287. Element Appearing More Than 25% In Sorted Array
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.
Example 1:
Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
Example 2:
Input: arr = [1,1]
Output: 1
Constraints:
1 <= arr.length <= 104
0 <= arr[i] <= 105
class Solution {
public int findSpecialInteger(int[] a) {
HashMap<Integer,Integer> nm=new HashMap<>();
int i,j=Integer.MIN_VALUE,k=Integer.MIN_VALUE;
for(i=0;i<a.length;i++)
{
if(nm.containsKey(a[i]))
nm.put(a[i] , nm.get(a[i])+1);
else
nm.put(a[i],1);
}
for(int m : nm.keySet())
{
if(nm.get(m)>k)
{
k=nm.get(m);
j=m;
}
}
return j;
}
}