Check if two arrays have the same contents (in any order)
This doesn't require conversion to set:
a.sort == b.sort
for two arrays A and B:
A and B have same contents if:
(A-B).blank? and (B-A).blank?
or you can just check for:
((A-B) + (B-A)).blank?
Also as suggested by @cort3z this solution als0 works for polymorphic arrays i.e
A = [1 , "string", [1,2,3]]
B = [[1,2,3] , "string", 1]
(A-B).blank? and (B-A).blank? => true
# while A.uniq.sort == B.uniq.sort will throw error `ArgumentError: comparison of Fixnum with String failed`
::::::::::: EDIT :::::::::::::
As suggested in the comments, above solution fails for duplicates.Although as per the question that is not even required since the asker is not interested in duplicates(he is converting his arrays to set before checking and that masks duplicates and even if you look at the accepeted answer he is using a .uniq operator before checking and that too masks duplicates.). But still if duplicates interests you ,Just adding a check of count will fix the same(as per the question only one array can contain duplicates). So the final solution will be:
A.size == B.size and ((A-B) + (B-A)).blank?