Find a unique element in a compound array

后端 未结 7 1343
面向向阳花
面向向阳花 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:14

    Building on dwenzel's idea:

    airports = [['LAX', 'BWI'], ['BOS', 'SEA'], ['HNL', 'LAX'], ['SEA', 'HNL']]
    
    departures, arrivals = airports.transpose
    
    first_departure_index = departures.index{|dep| !arrivals.include?(dep)}
    

提交回复
热议问题