Skip to content

Commit 481f542

Browse files
committed
Find First and Last in Sorted Array complete
1 parent 02e17c2 commit 481f542

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var searchRange = function (nums, target) {
2+
let result = [-1, -1];
3+
for (let i = 0; i < nums.length; i++) {
4+
if (nums[i] === target) {
5+
if (result[0] === -1) {
6+
result = [i, i];
7+
} else {
8+
result = [result[0], i];
9+
}
10+
}
11+
}
12+
return result;
13+
};
14+
15+
const arr = [5, 7, 7, 8, 8, 10];
16+
17+
console.log(searchRange(arr, 8));

0 commit comments

Comments
 (0)