This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const startAndStopPosition = (nums, val) => { | |
if(!Array.isArray(nums) || nums.length === 0) return [-1, -1]; | |
// Initialization of Variables | |
let initialIndex = 0; | |
let count = 0; | |
let startAndStop = []; | |
// Looping through the array; | |
for(let i = 0; i < nums.length; i++) { | |
if(nums[i] < val) { | |
initialIndex += 1; | |
} | |
if(nums[i] === val) { | |
count += 1; | |
} | |
} | |
if(count === 0) { | |
startAndStop.push(-1, -1); | |
}else { | |
startAndStop.push(initialIndex, (count - 1) + initialIndex); | |
} | |
return startAndStop; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much @meekg33k this really helped open my mind.......
Looking forward to the next challenge