RedOster RedOster - 3 years ago 97
Ruby Question

Filter array of arrays with an array in Ruby

Looking for a more efficient way of filtering an array of arrays using another array in Ruby. Let me demonstrate. Starting with this:

core = [[1, "apple", "James Bond"],
[5, "orange", "Thor"],
[10, "banana", "Wolverine"],
[15, "orange", "Batman"],
[20, "apple", "Mickey Mouse"],
[25, "orange", "Lee Adama"],
[30, "banana", "Luke Skywalker"]]

filter = ["apple", "banana"]

result = core.magical_function(filter)

# result == [[5, "orange", "Thor"],
# [15, "orange", "Batman"],
# [25, "orange", "Lee Adama"]]


The only thing I can think of is looping through the filter elements, but this slows down my code a lot when this toy example gets more complicated.

Answer Source

Use Array#reject and Array#include?:

core.reject { |_,fruit,_| filter.include?(fruit) }
  # => [[5, "orange", "Thor"], [15, "orange", "Batman"], [25, "orange", "Lee Adama"]]

If filter is large, first convert it to a set for faster lookups:

require 'set'
filter_set = filter.to_set
core.reject { |_,fruit,_| filter_set.include?(fruit) }

See Set and its instance methods. When required, set adds the instance method to_set to the module Enumerable which is inclueded by Array. Ruby implements sets with unseen hashes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download