Move Element To End

  • N, 1

function moveElementToEnd(arr, toMove) {
  let e = arr.length - 1;
  let s = 0;

  while (s < e) {
    
    while (s < e && arr[e] === toMove) {
      e -= 1;
    }

    if (arr[s] === toMove) {
      [arr[e], arr[s]] = [arr[s], arr[e]];
    }

    s += 1;
  }

  return arr;
}

// Do not edit the line below.
exports.moveElementToEnd = moveElementToEnd;

Last updated