문제 링크 : Find First and Last Position of Element in Sorted Array
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function(nums, target) {
let result = [-1, -1]
if(nums.includes(target)) {
nums.forEach( (e, idx) => {
if(e === target && result[0] === -1){
result = [idx, idx]
}else if( e=== target && result[0] !== -1) {
result[1] = idx
}
})
}
return result;
};
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function(nums, target) {
let start = -1, end = -1;
for (let i=0; i<nums.length; i++) {
if (target === nums[i]) {
if (start === -1) {
start = i;
}
end = i;
}
}
return [start, end];
};