Created
March 4, 2018 10:03
-
-
Save Nditah/44fb6b0665f0ad930aa0b638bb1ba09d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/gudazul
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/** | |
How to Fine the missing element in a sequential array without looping | |
*/ | |
function solution(A = []) { | |
let N=A.length; | |
if(N == 1) { | |
if(A[0] !== 1 ) return 1 ; | |
return 2 ; | |
} | |
if(N > 1 ){ | |
// console.log("length " + N); | |
let sum = ((2 + N)*(N+1)/2 ); | |
let left = (A.reduce(function(a, b) { return a + b; }, 0)) ; | |
// console.log("Sum "+ sum +" Left"+ left); | |
return sum - left ; | |
/* | |
for(let i = 1 ; i < N+ 2; i++){ | |
//if(!A.includes(i)) return i; | |
if(A.indexOf(i) === -1) return i; | |
} | |
*/ | |
}else{ | |
return 1; | |
} | |
} | |
let B = [1,2,3,4,5,7,8]; | |
let A = []; | |
A[0] = 4; | |
A[1] = 3; | |
A[2] = 1; | |
A[3] = 5; | |
A[4] = 6; | |
console.log(solution([9])) ; //4 | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
/** | |
How to Fine the missing element in a sequential array without looping | |
*/ | |
function solution(A = []) { | |
let N=A.length; | |
if(N == 1) { | |
if(A[0] !== 1 ) return 1 ; | |
return 2 ; | |
} | |
if(N > 1 ){ | |
// console.log("length " + N); | |
let sum = ((2 + N)*(N+1)/2 ); | |
let left = (A.reduce(function(a, b) { return a + b; }, 0)) ; | |
// console.log("Sum "+ sum +" Left"+ left); | |
return sum - left ; | |
/* | |
for(let i = 1 ; i < N+ 2; i++){ | |
//if(!A.includes(i)) return i; | |
if(A.indexOf(i) === -1) return i; | |
} | |
*/ | |
}else{ | |
return 1; | |
} | |
} | |
let B = [1,2,3,4,5,7,8]; | |
let A = []; | |
A[0] = 4; | |
A[1] = 3; | |
A[2] = 1; | |
A[3] = 5; | |
A[4] = 6; | |
console.log(solution([9])) ; //4</script></body> | |
</html> |
This file contains hidden or 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
/** | |
How to Fine the missing element in a sequential array without looping | |
*/ | |
function solution(A = []) { | |
let N=A.length; | |
if(N == 1) { | |
if(A[0] !== 1 ) return 1 ; | |
return 2 ; | |
} | |
if(N > 1 ){ | |
// console.log("length " + N); | |
let sum = ((2 + N)*(N+1)/2 ); | |
let left = (A.reduce(function(a, b) { return a + b; }, 0)) ; | |
// console.log("Sum "+ sum +" Left"+ left); | |
return sum - left ; | |
/* | |
for(let i = 1 ; i < N+ 2; i++){ | |
//if(!A.includes(i)) return i; | |
if(A.indexOf(i) === -1) return i; | |
} | |
*/ | |
}else{ | |
return 1; | |
} | |
} | |
let B = [1,2,3,4,5,7,8]; | |
let A = []; | |
A[0] = 4; | |
A[1] = 3; | |
A[2] = 1; | |
A[3] = 5; | |
A[4] = 6; | |
console.log(solution([9])) ; //4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment