Desmos, 227 215 bytes
z(A)=length(A)
h(l,a,k)=\left\{l[a]=k:0,1\right\}
f(a,b)=\sum_{n=1}^{z(b)}(1-h(b,n,a))
s=f(A,B)
j(A,B)=A[\sum_{N=1}^{z(s)}N(1-h(s,N,0))\prod_{a=1}^{N-1}h(s,a,0)...z(A)]
p=j(A,B)
q=j(p[z(p)...1],B)
m(A,B)=q[z(q)...1]
Try It On Desmos!
Try It On Desmos!(Prettified Version)
Final function is \$m(a,b)\$, where \$a\$ is the list that is being trimmed, and \$b\$ is the list of "falsey" values that are trimmed from \$a\$.
Much harder than I initially anticipated because Desmos has limited list functionality. Hopefully it's fine if I output an one-element list with the one element being undefined
([undefined]
) when the output is supposed to be an empty list([]
). If it isn't fine then tell me and I can fix(but it would cost more bytes ☹️)
Note: Not too sure why, but you can't directly paste the code into Desmos for some reason. You have to paste each expression one at a time in order for it to work.