Category
Replace all occurrences of searchElement with replacement in an array. Strict equality (===) is used to compare elements.
searchElement
replacement
===
Time complexity: O(n)
Space complexity: O(1)
The array to replace elements in
The element to search for
The element to replace searchElement with
The number of elements replaced
Array
const array = [1, 2, 1, 3, 1];replaceAll(array, 1, 2);console.log(array); // [2, 2, 2, 3, 2] Copy
const array = [1, 2, 1, 3, 1];replaceAll(array, 1, 2);console.log(array); // [2, 2, 2, 3, 2]
Replace all occurrences of
searchElement
withreplacement
in an array. Strict equality (===
) is used to compare elements.Time complexity: O(n)
Space complexity: O(1)