Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Jan 1, 2024
1 parent d007f2e commit ab5a0c8
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* You're given an array of integers and an integer. Write a function that
* moves all instances of that integer in the array to the end of the
* array and returns the array.
* The function should perform this in place (i.e., it should mutate the input array)
* and doesn't need to maintain the order of the other integers.
*
* Input:
* array = [2, 1, 2, 2, 2, 3, 4, 2]
* toMove = 2
*
* Output:
* [1, 3, 4, 2, 2, 2, 2, 2]
*/

function moveElementToEnd(array, toMove) {
let count = 0;
let pointer = 0;

for (let index = 0; index < array.length; index++) {
if (array[index] === toMove) count++;
else {
array[pointer] = array[index];
pointer++;
}
}

for (let index = pointer; index < array.length; index++) {
array[index] = toMove;
}

return array;
}

0 comments on commit ab5a0c8

Please sign in to comment.