Skip to content

Instantly share code, notes, and snippets.

@sayeed1999
Created July 14, 2022 16:39
Show Gist options
  • Save sayeed1999/8ea86f8a8c15e41e02107b48784369eb to your computer and use it in GitHub Desktop.
Save sayeed1999/8ea86f8a8c15e41e02107b48784369eb to your computer and use it in GitHub Desktop.
/// 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