Skip to content

Commit 9ddfdfb

Browse files
committed
Add week 6 solutions : searchInRotatedSortedArray
1 parent 496d9e4 commit 9ddfdfb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var search = function(nums, target) {
5+
// iterate through each element.
6+
for (let i = 0; i < nums.length; i++) {
7+
// check if the current element is equal to the target.
8+
if (nums[i] === target) {
9+
// if found, return the index.
10+
return i;
11+
}
12+
}
13+
14+
// if the loop completes without finding the target, return -1.
15+
return -1;
16+
};

0 commit comments

Comments
 (0)