# Move Element To End

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-b2af35ffcfea0602ec7d999b17425627bc2b033b%2FScreenshot%202023-01-21%20at%2016.11.38.png?alt=media)

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