- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissingNumber.js
More file actions
Latest commit
27 lines (24 loc) · 1.19 KB
/
Copy pathmissingNumber.js
File metadata and controls
27 lines (24 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
functionmissingNumber(arr){
letcurrent,nextVal;
// this replaces indices with positive values and values less than or equal to the array length in order
for(leti=0;i<arr.length;i++){
if(arr[i]<=0||arr[i]>arr.length)continue;// ignore negative numbers or numbers greater than array length;
current=arr[i];
while(arr[current-1]!==current){// ex: arr[2 - 1] !== 2 meaning the arr[current value - 1] is not equal to the current value
nextVal=arr[current-1];// assign the next value to arr[val - 1];
arr[current-1]=current;
current=nextVal;
if(current<=0||current>arr.length)break;// break if negative or greater than array length
}
}
for(leti=0;i<arr.length;i++){
if(arr[i]!==i+1){
returni+1;
}
}
returnarr.length+1;// this means that all numbers are in order and there is no missing value in the array;
}
console.log(missingNumber([2,3,7,6,8,-1,-10,15]));// 1
console.log(missingNumber([1,3,6,4,1,2]));// 5
console.log(missingNumber([1,2,3]));// 4
console.log(missingNumber([-1,-3]));// 1