Skip to content

Commit 6bd3118

Browse files
committed
Add solution #1287
1 parent 7266b8a commit 6bd3118

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 1287. Element Appearing More Than 25% In Sorted Array
3+
* https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array sorted in non-decreasing order,
7+
* there is exactly one integer in the array that occurs more than 25% of the time.
8+
*
9+
* Return that integer.
10+
*/
11+
12+
/**
13+
* @param {number[]} arr
14+
* @return {number}
15+
*/
16+
var findSpecialInteger = function(arr) {
17+
const limit = arr.length * 0.25;
18+
const offset = limit % 1 === 0 ? limit : Math.ceil(limit) - 1;
19+
20+
for (let i = 0; i < arr.length; i++) {
21+
if (arr[i] === arr[i + offset]) {
22+
return arr[i];
23+
}
24+
}
25+
};

0 commit comments

Comments
 (0)