From: 7stud -- Date: 2009-08-25T03:12:19+09:00 Subject: Re: INNER JOINING Two Arrays I'd do something like this: b=[1,1,2,2,2,5,13,13,13] data = Hash.new{|hash, key| hash[key] = []} b.each_with_index do |num, i| data[num] << i end a=[1,2,5,13] indexes = [] items = [] a.each_with_index do |num, i| indexes_for_num = data[num] arrs = indexes_for_num.map{|index| [i, index]} indexes.push(*arrs) indexes_for_num.length.times{items << [num, num]} end p indexes p items --output:-- [[0, 0], [0, 1], [1, 2], [1, 3], [1, 4], [2, 5], [3, 6], [3, 7], [3, 8]] [[1, 1], [1, 1], [2, 2], [2, 2], [2, 2], [5, 5], [13, 13], [13, 13], [13, 13]] For small arrays, like the ones in your example, the speeds will be close, but as the arrays get bigger, the hash lookups will be much faster, like 10 times faster. -- Posted via http://www.ruby-forum.com/.