Category
Replace the first occurrence 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 index of the replaced element, or -1 if it is not present.
-1
Array
const array = [1, 2, 1, 3, 1];replace(array, 1, 2);console.log(array); // [2, 2, 1, 3, 1] Copy
const array = [1, 2, 1, 3, 1];replace(array, 1, 2);console.log(array); // [2, 2, 1, 3, 1]
Replace the first occurrence of
searchElement
withreplacement
in an array. Strict equality (===
) is used to compare elements.Time complexity: O(n)
Space complexity: O(1)