We use array comparison and set operations to find common, different, or combined items between lists. This helps us organize and compare data easily.
Array comparison and set operations in Ruby
array1 & array2 # Intersection: items common to both arrays array1 | array2 # Union: all unique items from both arrays array1 - array2 # Difference: items in array1 not in array2 array1 == array2 # Equality: checks if arrays have same items in same order array1.eql?(array2) # Strict equality including type and order
The & operator finds items present in both arrays.
The | operator combines arrays but removes duplicates.
a = [1, 2, 3] b = [2, 3, 4] puts a & b # Output: [2, 3]
a = [1, 2, 3] b = [3, 4, 5] puts a | b # Output: [1, 2, 3, 4, 5]
a = [1, 2, 3] b = [2, 3] puts a - b # Output: [1]
a = [1, 2, 3] b = [1, 2, 3] puts a == b # Output: true
This program shows how to compare two arrays using intersection, union, difference, and equality.
array1 = [1, 2, 3, 4] array2 = [3, 4, 5, 6] puts "Array 1: #{array1}" puts "Array 2: #{array2}" intersection = array1 & array2 union = array1 | array2 difference = array1 - array2 are_equal = array1 == array2 puts "\nIntersection (common items): #{intersection}" puts "Union (all unique items): #{union}" puts "Difference (items in array1 not in array2): #{difference}" puts "Are arrays equal? #{are_equal}"
Time complexity for these operations depends on array size, usually O(n*m) for intersection and difference.
Space complexity is O(n + m) because new arrays are created for results.
Common mistake: Using == checks order and content, not just content. Use set operations for content comparison.
Use intersection when you want common items, difference to find missing items, and union to combine without duplicates.
Array comparison helps find common, different, or combined items between lists.
Use & for intersection, | for union, and - for difference.
Equality == checks if arrays have the same items in the same order.
Practice
Which operator in Ruby returns the common elements between two arrays?
[1, 2, 3] ? [2, 3, 4]Solution
Step 1: Understand the operators
The&operator returns elements common to both arrays.Step 2: Apply to example arrays
For arrays [1, 2, 3] and [2, 3, 4], common elements are 2 and 3.Final Answer:
& (intersection) -> Option AQuick Check:
Common elements = & [OK]
- Confusing union (|) with intersection (&)
- Using - for common elements
- Thinking == compares elements regardless of order
Which of the following is the correct syntax to find the difference between two arrays a and b in Ruby?
Solution
Step 1: Recall difference operator
The-operator returns elements inanot inb.Step 2: Confirm syntax correctness
Usinga - bis the correct Ruby syntax for difference.Final Answer:
a - b -> Option BQuick Check:
Difference = - [OK]
- Using & instead of - for difference
- Using | which is union
- Using == which checks equality
What is the output of the following Ruby code?
a = [1, 2, 3, 4] b = [3, 4, 5, 6] result = a | b puts result.inspect
Solution
Step 1: Understand the union operator
The|operator combines all unique elements from both arrays.Step 2: Apply union to arrays
Arrays a and b combined uniquely are [1, 2, 3, 4, 5, 6].Final Answer:
[1, 2, 3, 4, 5, 6] -> Option AQuick Check:
Union = | [OK]
- Confusing union with intersection
- Expecting duplicates in output
- Using - which removes elements
Identify the error in the following Ruby code snippet:
a = [1, 2, 3] b = [2, 3, 4] result = a & b puts result
What is the problem?
Solution
Step 1: Check syntax and operators
The&operator is valid for arrays to find intersection.Step 2: Verify output behavior
The code outputs common elements [2, 3] without error.Final Answer:
No error; code runs and outputs common elements -> Option DQuick Check:
Intersection & works fine [OK]
- Thinking & causes syntax error
- Expecting union instead of intersection
- Confusing output with difference
You have two arrays representing user IDs:
active_users = [101, 102, 103, 104, 105] inactive_users = [103, 105, 107]
Which Ruby expression gives you the list of users who are active but not inactive?
Solution
Step 1: Understand the problem
We want users in active_users but not in inactive_users.Step 2: Use difference operator
The-operator returns elements in first array not in second.Step 3: Apply to given arrays
active_users - inactive_users = [101, 102, 104]Final Answer:
active_users - inactive_users -> Option CQuick Check:
Difference = - [OK]
- Using & which gives only common users
- Using | which combines all users
- Using == which checks equality
Ready to prove it?
Pass the test and this pattern moves unknown
