Created
February 16, 2021 08:28
-
-
Save MohammedALREAI/33581621a1dbac12879c09837ff5d2a4 to your computer and use it in GitHub Desktop.
moveToEnd AlgoExport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function moveToEnd(arr:Array<number>,target:number){ | |
if(arr.includes(target)) return arr | |
let p1=0 | |
let p2=arr.length-1 | |
while(p1<p2){ | |
while(arr[p2]===target) p2--; | |
if(arr[p1]=== target ){ | |
swap(arr,p1,p2); | |
p1++ | |
} | |
} | |
return arr | |
} | |
function swap(arr:Array<number>,i:number,j:number){ | |
let temp=arr[i]; | |
arr[i]=arr[j] | |
arr[j]=temp | |
} | |
console.log(moveToEnd([1,2,3,2,2,5,2],8) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment