I'm trying to figure out an efficient way to remove duplicates from an array of array of string but not within a single array, but from all array.
It's hard to explain so let me show an example:
[
["Word1", "Word2", "Word3", "Word4"],
["Word1", "Word5", "Word3", "Word4"],
["Word1", "Word2", "Word3", "Word7"],
]
Expected Results:
[
["Word2", "Word4"],
["Word5", "Word4"],
["Word2", "Word7"],
]
Index 0: Removed because all Index 0 are identicals.
Index 1: Kept because not all Index 1 are identicals. and so on...
The closer I could come up is
def clean_duplicates(attributes)
valid_attributes = attributes.map { [] }
attributes.first.count.times.each do |i|
next if attributes.all? { |v_attrs| v_attrs[i] == attributes.last[i] }
attributes.each_with_index do |_, v|
valid_attributes[v].push(attributes[v][i])
end
end
valid_attributes
end
clean_attributes([["Word1", "Word2", "Word3", "Word4"], ["Word1", "Word5", "Word3", "Word4"], ["Word1", "Word2", "Word3", "Word7"]])
=> [["Word2", "Word4"], ["Word5", "Word4"], ["Word2", "Word7"]]
Is there a better way?
Thank you!