Find a unique element in a compound array

后端 未结 7 1334
面向向阳花
面向向阳花 2021-01-26 12:34

I am trying to solve a problem where I need to find the airport code in an array of arrays of that represents the starting point of a multi-city flight plan. For example: Given

相关标签:
7条回答
  • 2021-01-26 13:25
    def find_start(arr)
      flat = arr.flatten
      first, second = flat.reject { |val| flat.count(val) > 1}
      start_city = if flat.index(first) % 2 == 0 #if index of first is even
                     first
                   else
                     second
                   end
      arr.find { |pair| pair[0] == start_city }
    end
    
    0 讨论(0)
提交回复
热议问题