> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/move-element-to-end.md).

# Move Element To End

![](/files/hxLjj3utwEoNdb4Wfr3m)

* N, 1

```jsx
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;
```
