Ahmad hamza Ahmad hamza - 22 days ago 7
Ruby Question

Remove duplicates from an array of array

How do I remove duplicates from this array?

product_areas = [["1", "2", "3"], ["3", "1", "2"]]


I have tried
product_areas.uniq!
,
product_area.uniq
but the same thing is repeating. What am I missing here?

Expected Output:

product_areas = ["1", "2", "3"]

Answer Source

Try this:

product_areas = [["1", "2", "3"], ["3", "1", "2"]].flatten.uniq

Using flatten on your array will create the following result:

["1", "2", "3", "3", "1", "2"]

When you call uniq on that array, you will get the result you were expecting:

["1", "2", "3"]