Logo

Programming-Idioms

Set all the elements in the array x to the same value v
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#include <array>
x.fill(v);
x = v
for i := range x {
	x[i] = v
}
func fill[T any](x []T, v T) {
	for i := range x {
		x[i] = v
	}
}
import static java.util.Arrays.fill;
fill(x, v);
for i := Low(x) to High(x) do x[i] := v;
my @x = ($v) x $n;
my @x = (undef) x $n; 

foreach (@x) { $_ = $v }
set_all_elements_to_same_value(X, V) :-
    length(X, Length),
    length(FilledList, Length),
    maplist(=(V), FilledList, X).
x[:] = [v] * len(x)
x.fill(v)
x.fill(v);