Created
July 14, 2022 16:39
-
-
Save sayeed1999/8ea86f8a8c15e41e02107b48784369eb to your computer and use it in GitHub Desktop.
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
/// Problem 1. | |
function closestNumber(n, m) { | |
let i = n-1; | |
let j = n+1; | |
while(1==1) { | |
if(i%m===0 && j%m!==0) { | |
return i; | |
} | |
else if(i%m!==0 && j%m===0) { | |
return j; | |
} | |
else if (i%m===0 && j%m===0) { | |
if(i>=0) { | |
return j; | |
} | |
if(j>=-i) { | |
return j; | |
} | |
return i; | |
} | |
i--; | |
j++; | |
} | |
} | |
// console.log(closestNumber(13, 4)); | |
// console.log(closestNumber(15, 4)); | |
/// Problem 2. | |
function merge(a, b) { | |
let lena = a.length; | |
let lenb = b.length; | |
let result = ""; | |
let minlen = lena; | |
if(lenb < lena) minlen = lenb; | |
for(let i=0; i<minlen; i++) { | |
result += a[i] + b[i]; | |
} | |
if(lena > lenb) { | |
result += a.substr(lenb); | |
} | |
else if(lena < lenb) { | |
result += b.substr(lena); | |
} | |
return result; | |
} | |
// let res = merge("", "asdsa"); | |
// console.log(res); | |
/// Problem 3. | |
function remove_duplicate(array, size) { // input: [1,2,2], 3 | |
let len = array.length; | |
let j = 0; | |
for(let i=1; i<len; i++) { | |
if(array[i] !== array[j]) { | |
j++; | |
array[j] = array[i]; | |
} | |
} | |
array = array.slice(0, j+1); | |
return array; | |
} | |
// console.log(remove_duplicate([1, 2, 2, 2, 2], 5)); | |
// console.log(remove_duplicate([2, 2, 2, 2, 2], 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment