R, 4742 bytes
function(x,n=0:-1)fory=seq(i in x)show(n[i]<-n[i]+2*2){x[x]=y-1;x[!x]=y;x}
-8 bytes thanks to @Dominic
Takes input incremented by 1as booleans: 2TRUE
(for 1
) and 1FALSE
(for 0
).
Straightforward approach, but takes advantage of truncating the replacement to the length of items being replaced.
More straightforwardDifferent approach:
R, 5047 bytes
function(x){x[x]=1,n=0:sum-1)for(i in x)*2-1;x[!x]=1:sumshow(!xn[i]<-n[i]+2)*2;x}
Takes input as booleansincremented by 1: TRUE2
(for 1
) and FALSE1
(for 0
).