2
a=[1,2,3,4,5]
b=[4,3]
array_wanted=[4,3,1,2,5]

I could do this via a mapping and pushing, but i would love to know more elegant ways of doing this.

1 Answer 1

6
(b & a) + (a - b)
# => [4, 3, 1, 2, 5]

And if you are sure that all elements from b are present in a, union operator | seems to return the proper order:

b | a
# => [4, 3, 1, 2, 5]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.