> pairings[pos].delete(pair) > [...] > pairings[pos].unshift(pair) I just realized that this behaves differently in ruby18 and ruby19 when used within an each loop. These lines could be removed. Also, my submission generates too many pairs. The following sorts the pairs so that a perfect match is included only once. names.each do |k| data[k].each_with_index do |l, v| w = v ** 2 + data[l].index(k) ** 2 maxpos = w if w > maxpos pair = [k, l].sort pairings[w] << pair unless pairings[w].include?(pair) end end In any case, my mostly naive approach lags far behind Eric M and Eric I's solutions (which I find quite interesting BTW).