I have two arrays a and b which I need to iterate through simultaneously
creating essentially an “INNER JOIN” on the values of the two arrays.
Both arrays contain integers and are sorted in ascending order. The
first value in each array is the same and the last value in each array
is the same. On each iteration through the arrays I need to obtain the
indexes of each array if the array values are equal to each other.
This is the code I have which works:
a=[1,2,5,13]
b=[1,1,2,2,2,5,13,13,13]
indexs=[]
items=[]
a.each_with_index do |itema,indexa|
 b.each_with_index do |itemb,indexb|
 if (itema==itemb) then
  puts “#{indexa} #{indexb}”
  indexs << [indexa,indexb]
  items << [itema,itemb]
 else
  next
 end
 end
end
producing this output:
0 0
0 1
1 2
1 3
1 4
2 5
3 6
3 7
3 8
and these values of indexs and items:
irb(main):017:0> indexs
=> [[0, 0], [0, 1], [1, 2], [1, 3], [1, 4], [2, 5], [3, 6], [3, 7], [3,
8]]
irb(main):018:0> items
=> [[1, 1], [1, 1], [2, 2], [2, 2], [2, 2], [5, 5], [13, 13], [13, 13],
[13, 13]]
Q1: Is there a more idiomatic and concise way of doing this using
functional array methods?Â
Also note that if you interchanged the two each loops they would produce
the same output and results:
b.each_with_index do |itemb,indexb|
 a.each_with_index do |itema,indexa|
 …
 end
end
Q2: Are there any observations one could make about which scenario would
be faster both for my code and a more idiomatic solution you might have?