Skip to content

Instantly share code, notes, and snippets.

@MohammedALREAI
Created April 9, 2021 21:57
Show Gist options
  • Save MohammedALREAI/891e80b8694983819efefc4b07766846 to your computer and use it in GitHub Desktop.
Save MohammedALREAI/891e80b8694983819efefc4b07766846 to your computer and use it in GitHub Desktop.
arrayReplace
function arrayReplace(inputArray: number[], elemToReplace: number, substitutionElem: number): number[] {
let arr:number[]=[]
let count=inputArray.filter(x=>x===elemToReplace).length
if(count<1){
return inputArray
}
for(let i=0;i<inputArray.length ;i++){
let nu=0
if(inputArray[i] ===elemToReplace &&count>=nu){
inputArray[i]=substitutionElem
nu++
}
}
return inputArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment