当前位置:网站首页>1287. Elements that appear more than 25% in an ordered array
1287. Elements that appear more than 25% in an ordered array
2022-07-03 16:07:00 【what's your name.】
【 Simple 】 Give you a non decreasing Orderly An array of integers , It is known that there is exactly one integer in this array , It occurs more than the total number of elements in the array 25%.
Please find and return this integer
Example :
Input :arr = [1,2,2,6,6,6,6,7,10]
Output :6
Tips :
1 <= arr.length <= 10^4
0 <= arr[i] <= 10^5
Solution 1 :
public int m1(int[] arr) {
int target = 0;
int start = 0;
int x = arr.length / 4;//25%
for (int i = 0; i < arr.length; i++) {
if (arr[i] != target) {
start = i;
target = arr[i];
}
if (i - start + 1 > x)
return target;
}
return target;
}
Solution 2 :
public int m2(int[] arr) {
int threshold = arr.length / 4;//25%
for (int i = 0; i < arr.length; i++) {
if(arr[i + threshold] == arr[i])
return arr[i];
}
return 0;
}
Solution 3 : Hash map
Map<Integer, Integer> map = new HashMap();
for(int i : arr){
// First, store the data in the array in map in ,key Element value ,value Is the number of occurrences
map.put(i, map.getOrDefault(i, 0) + 1);
}
for(int key : map.keySet()){
Integer value = map.get(key);
if(value > arr.length / 4){
// Timely output if there are satisfied
return key;
}
}
return -1;
边栏推荐
- Detailed explanation of four modes of distributed transaction (Seata)
- Problems of CString in multithreading
- App mobile terminal test [3] ADB command
- 记一次jar包冲突解决过程
- 分布式事务(Seata) 四大模式详解
- Detailed pointer advanced 1
- 【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
- pycharm错Error updating package list: connect timed out
- The wonderful use of do{}while()
- "Remake Apple product UI with Android" (3) - elegant statistical chart
猜你喜欢
![[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)](/img/04/460734209ec315c5c02cb3fae4bf0e.png)
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)

【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示

Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14

Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)

Principles of several common IO models

Shell script import and export data

Reading notes of "micro service design" (Part 2)

记一次jar包冲突解决过程

Find mapping relationship

The mixlab editing team is recruiting teammates~~
随机推荐
Semi supervised learning
App mobile terminal test [4] APK operation
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
The mixlab editing team is recruiting teammates~~
Mixlab编辑团队招募队友啦~~
Location of software installation information and system services in the registry
Three dimensional reconstruction of deep learning
Download and install common programs using AUR
Function introduction of JMeter thread group
"Everyday Mathematics" serial 56: February 25
Detailed explanation of string function and string function with unlimited length
Please be prepared to lose your job at any time within 3 years?
Break through 1million, sword finger 2million!
Redis在Windows以及Linux系统下的安装
App移动端测试【5】文件的写入、读取
Expression of request header in different countries and languages
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Nine ways to define methods in scala- Nine ways to define a method in Scala?
[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)