I have two arrays:
list_a = [20, 20, 20, 21, 21, 22, 20,21, 23, 20, 23, 24]
list_b = [120, 121, 122, 123, 124]
and I want to create the next array from those two arrays like so:
newList = [120, 120, 120, 121, 121, 122, 120, 121, 123, 120, 123, 124]
so the first number from the first array will replaced by value with 120, but not in the length of the 20, and so on, and afterwards when 20 is also located, it will replace it with the first number also
I tried a lot of stuff, nothing worked…
4
Answers
The specs were not clear
Try this, we create a unique list of list_a and use the placement of each to index into list_b
There’s probably a more elegant solution with Sets but the first thing that came to my mind was to make a
hash map
first and thenmap
through thelist_a
array
.This solution allows for any values in
list_b
but requires that the following condition is met:Hope this helps or sends you in the right direction.
You could take an object for the values and keep an index of unseen values.