Skip to content

Instantly share code, notes, and snippets.

@fdjones
Created April 1, 2017 21:12
Show Gist options
  • Save fdjones/841204a536d19d2b0e5f7c481ff8be16 to your computer and use it in GitHub Desktop.
Save fdjones/841204a536d19d2b0e5f7c481ff8be16 to your computer and use it in GitHub Desktop.
// Task
// Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering.
// For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2]
// since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].
function deleteNth(arr,x){
for(var k = 0; k < arr.length; k++) {
var counter = 1;
var empty = [];
for(var i = k + 1; i < arr.length; i++) {
if(arr[k] === arr[i]) {
counter++;
if(counter > x) {
arr.splice(i, 1);
}
}
}
}
return arr;
}
// Some of the failed tests below:
Random tests
Testing for delete_nth([32, 3, 48, 32, 48, 33, 50, 24, 32, 33, 5, 32, 24, 37, 48, 33, 5, 48, 48, 48, 33, 24, 3, 24, 48, 5, 32, 33, 32, 24, 5, 48, 48,5)
✘ It should work on random inputs too! - Expected: '[32, 3, 48, 32, 48, 33, 50, 24, 32, 33, 5, 32, 24, 37, 48, 33, 5, 48, 48, 33, 24, 3, 24, 5, 32, 33, 24, 5]',
instead got: '[32, 3, 48, 32, 48, 33, 50, 24, 32, 33, 5, 32, 24, 37, 48, 33, 5, 48, 48, 33, 24, 3, 24, 5, 32, 33, 24, 5, 48]'
Completed in 1ms
Testing for delete_nth([13, 47, 13, 50, 29, 2, 13, 33, 5, 13, 29, 50, 33, 33, 13, 29, 33, 47, 13, 41, 41, 2, 29, 41, 29, 29, 47, 33, 5, 41, 5, 2, 29, 5, 2, 29, 41, 33, 50, 41, 33,3)
✘ It should work on random inputs too! - Expected: '[13, 47, 13, 50, 29, 2, 13, 33, 5, 29, 50, 33, 33, 29, 47, 41, 41, 2, 41, 47, 5, 5, 2, 50]',
instead got: '[13, 47, 13, 50, 29, 2, 13, 33, 5, 29, 50, 33, 33, 29, 47, 41, 41, 2, 41, 29, 47, 5, 5, 2, 50]'
Completed in 1ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment