Skip to content

Commit 2bed37d

Browse files
solves element appearing more than 25% in sorted array
1 parent 228d529 commit 2bed37d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
| 1271 | 🔒 [Hexspeak](https://leetcode.com/problems/hexspeak) | | |
332332
| 1275 | [Find Winner On a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game) | [![Java](assets/java.png)](src/FindWinnerOnATicTacToeGame.java) | |
333333
| 1281 | [Subtract the Product and Sum of Digits of a Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer) | [![Java](assets/java.png)](src/SubtractTheProductAndSumOfDigitsOfAnInteger.java) | |
334-
| 1287 | [Element Appearing More Than 25% in Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array) | | |
334+
| 1287 | [Element Appearing More Than 25% in Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array) | [![Java](assets/java.png)](src/ElementAppearingMoreThan25PercentInSortedArray.java) | |
335335
| 1290 | [Convert Binary Number In A Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer) | | |
336336
| 1295 | [Find Numbers With Even Numbers of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits) | | |
337337
| 1299 | [Replace Elements With Greatest Element on Right Side](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side) | | |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class ElementAppearingMoreThan25PercentInSortedArray {
2+
public int findSpecialInteger(int[] array) {
3+
if (array.length <= 3) return array[0];
4+
final int specialIntegerFrequency = array.length / 4 + 1;
5+
for (int i = 1, current = 1 ; i < array.length ; i++) {
6+
if (array[i] ==array[i - 1]) current++;
7+
else current = 1;
8+
9+
if (current >= specialIntegerFrequency) return array[i];
10+
}
11+
return -1;
12+
}
13+
}

0 commit comments

Comments
 (0)