Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7bd5d5

Browse files
committed
Add solution on js for 1287
1 parent 9d631e5 commit e7bd5d5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
1287. Element Appearing More Than 25% In Sorted Array
2+
3+
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.
4+
5+
```Javascript
6+
const findSpecialInteger = function(arr) {
7+
let count = 0;
8+
let item = -1;
9+
for (var i = 0; i < arr.length; i++) {
10+
if (item == arr[i]) {
11+
count++;
12+
} else {
13+
item = arr[i];
14+
count = 1;
15+
}
16+
if (count > arr.length * 0.25) {
17+
return item;
18+
}
19+
}
20+
return item;
21+
};
22+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const findSpecialInteger = function(arr) {
2+
let count = 0;
3+
let item = -1;
4+
for (var i = 0; i < arr.length; i++) {
5+
if (item == arr[i]) {
6+
count++;
7+
} else {
8+
item = arr[i];
9+
count = 1;
10+
}
11+
if (count > arr.length * 0.25) {
12+
return item;
13+
}
14+
}
15+
return item;
16+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /