Closed joshmfrankel closed 1 year ago
array_1 = [1, 2, 3] array_2 = [2, 3, 1] array_with_duplicates = [1, 1, 1, 3, 3, 2] # Equality (with order importance) array_1 == [1, 2, 3] #=> true array_1 == array_2 #=> false # Spaceship operator returns: 0 (equal), -1 (elements less than other array), 1 (elements more than other array) (array_1 <=> array_2) == 0 #=> false # Equality (without order importance) array_1.sort == array_2.sort #> true (array_1.sort <=> array_2.sort) == 0 #=> true array_1.sort == array_with_duplicates.sort #> false # Equality (without duplicates) array_1.uniq.sort == array_with_duplicates.uniq.sort #> true # Iterations, Shiterations # Both uniq and sort will iterate the entire collection, adding to processing time. # `#uniq` - "self is traversed in order, and the [first](https://apidock.com/ruby/Array/first) occurrence is kept." # `#sort` - "Comparisons for the [sort](https://apidock.com/ruby/Array/sort) will be done using the <=> operator" # Equality (without duplicates and ordering) Set.new(array_1) == Set.new(array_2) #=> true Set.new(array_1) == Set.new(array_with_duplicates) #=> true # Array intersection (unique values only ordered by first collection) # https://ruby-doc.org/3.2.2/Array.html#method-i-26 (array_1 & array_2) == array_1 #=> true (array_1 & array_with_duplicates) == array_1 #=> true
https://stackoverflow.com/questions/8916416/ruby-logical-operators-elements-in-one-but-not-both-arrays https://apidock.com/ruby/v2_5_5/Array/uniq https://apidock.com/ruby/v2_5_5/Array/sort%21