Skip to content

Instantly share code, notes, and snippets.

@ugate
Last active July 16, 2020 20:31
Show Gist options
  • Select an option

  • Save ugate/639fc90e2733f835ab1fddb103c2914a to your computer and use it in GitHub Desktop.

Select an option

Save ugate/639fc90e2733f835ab1fddb103c2914a to your computer and use it in GitHub Desktop.
Coding Challenges (Arrays)
/**
* Checks an array for duplicates
* @param {*[]} arr The array to check for duplicates
* @returns {Boolean} True when there are duplicates, false when there are none
*/
function hasDuplicate(arr) {
for (let i = 0, j, ln = arr.length; i < ln; ++i) {
for (j = i + 1; j < ln; ++j) { console.log(arr[i], '===>', arr[j]);
if (arr[i] === arr[j]) return true;
}
}
return false;
}
// optimized
function containsDuplicate(nums) {
nums.sort();
for (let i = 1, prev = nums[0], curr, ln = nums.length; i < ln; ++i) {
curr = nums[i];
if (curr === prev) return true;
prev = curr;
}
return false;
};
/**
* Removes duplicate entries in an array (in-place w/o generating another array)
* @param {*[]} arr The array where duplicates will be removed
* @returns {Integer} The new length of the array after removal
*/
function removeDuplicates(arr) {
for (let i = 0, si = 0, cnt; i < arr.length; ++i) {
if (arr[i] === arr[i + 1]) continue;
cnt = i - si;
if (cnt > 0) {
arr.splice(si, cnt);
i -= cnt;
si = i + 1;
} else if (arr[i] !== arr[i + 1]) {
si++;
}
}
return arr.length;
}
// optimized
function removeDuplicates2(nums) {
let len = nums.length;
if (len === 1) return 1;
let index = 0;
let val = nums[0];
for(let i = 1; i < len; ++i) {
if (val === nums[i]) continue;
index++;
nums[index] = val = nums[i];
}
return index+1;
}
console.clear();
console.log(`===================================`);
var x = [1,1,1,2,2,2,3,4];
console.log(x);
removeDuplicates(x);
console.log('FINAL ===>', x);
console.log(`===================================`);
var x = [1,2,2,2,3,4,4];
console.log(x);
removeDuplicates(x);
console.log('FINAL ===>', x);
console.log(`===================================`);
var x = [1,1,1,2,3,3,4];
console.log(x);
removeDuplicates(x);
console.log('FINAL ===>', x);
console.log(`===================================`);
var x = [1,2,2,3,4];
console.log(x);
removeDuplicates(x);
console.log('FINAL ===>', x);
console.log(`===================================`);
var x = [1,1,2,3,4,4];
console.log(x);
removeDuplicates(x);
console.log('FINAL ===>', x);
/**
* Determines the total increase between numbers in an array
* @param {*[]} arr The array that will be evaluated
* @returns {Integer} The total increase
*/
function maxProfit(arr) {
let max = 0;
for (let i = 1, ln = arr.length; i < ln; ++i) {
if (arr[i] > arr[i - 1]) {
max += arr[i] - arr[i - 1];
}
}
return max;
}
console.clear();
var x = [7,1,5,3,6,4];
console.log(x);
console.log('EXPECTED 7 ===>', maxProfit(x));
console.log(`===================================`);
var x = [1,2,3,4,5];
console.log(x);
console.log('EXPECTED 4 ===>', maxProfit(x));
console.log(`===================================`);
var x = [7,6,4,3,1];
console.log(x);
console.log('EXPECTED 0 ===>', maxProfit(x));
console.log(`===================================`);
var x = [1,1,5,3,9,4];
console.log(x);
console.log('EXPECTED 10 ===>', maxProfit(x));
console.log(`===================================`);
var x = [6,2,3,4,7];
console.log(x);
console.log('EXPECTED 5 ===>', maxProfit(x));
/**
* Shifts an array to the right or left by a given amount via an array copy
* or in-place
* @param {*[]} arr The array to shift
* @param {Integer} amt The number of array elements to shift by
* @param {Boolean} [left] Truthy to shift to the left, otherwise, shifts
* elements to the right
* @param {Boolean} [copy] Truthy to make a copy of the array when shifting
* or falsy to shift the passed array in-place. __NOTE: The lower the `amt`
* the _faster_ the shift will be in-place versus making a copy. The reverse
* is also true- the higher the `amt` the _faster_ the copy will be versus
* shifting in-place.__
* @returns {*[]} Either the passed array (in-place) or a newly shifted array
* (copy)
*/
function shift(arr, amt, left, copy) {
if (copy) { // copy is faster
let count = amt > arr.length ? amt % arr.length : amt;
return arr.concat(arr.splice(0, (left ? count : arr.length - count)));
}
while (amt > 0) {
if (left) arr.push(arr.shift());
else arr.unshift(arr.pop());
--amt;
}
return arr;
}
console.clear();
var x = [1,2,3,4,5,6,7], amt = 2;
console.log('->', x);
console.log(`shift right by ${amt} (copy)`, shift([1,2,3,4,5,6,7], amt, false, true));
console.log(`shift right by ${amt} (in place)`, shift([1,2,3,4,5,6,7], amt));
console.log(`===================================`);
var x = [1,2,3,4,5,6,7], amt = 8;
console.log(`shift right by ${amt} (copy)`, shift([1,2,3,4,5,6,7], amt, false, true));
console.log(`shift right by ${amt} (in place)`, shift([1,2,3,4,5,6,7], amt));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment