I have the following arrays:
A = "cheddar".split(//) # ["c", "h", "e", "d", "d", "a", "r"]
B = "cheddaar".split(//) # ["c", "h", "e", "d", "d", "a", "a", "r"]
The A array is a subset of the B array. If the A array had another “d” element, it wouldn’t be a subset.
I want to compare and find if one is a subset of the other even if they have duplicates. The A – B or the A & B doesn’t capture the duplicates it just compares them and find them matched. So I wrote the following which it captures the duplicates:
B.each do |letter|
A.delete_at(A.index(letter)) rescue ""
end
p A.empty?
Is this the best way or can it be optimized?
A similar question was posted a few weeks ago and I got the accepted answer with something like:
Benchmark Update