Menu
Coddy logo textTech

Array Operators

Part of the Fundamentals section of Coddy's Ruby journey — lesson 71 of 88.

Ruby provides special operators that let you combine, compare, and manipulate arrays in powerful ways. These operators make it easy to work with multiple arrays at once.

The + operator concatenates two arrays into a new one:

a = [1, 2, 3]
b = [4, 5, 6]
combined = a + b

puts combined  # Outputs: 1, 2, 3, 4, 5, 6

The - operator removes elements from the first array that appear in the second:

all_numbers = [1, 2, 3, 4, 5]
to_remove = [2, 4]
result = all_numbers - to_remove

puts result  # Outputs: 1, 3, 5

The & operator returns elements common to both arrays (intersection):

a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
common = a & b

puts common  # Outputs: 3, 4

The | operator combines arrays and removes duplicates (union):

a = [1, 2, 3]
b = [3, 4, 5]
union = a | b

puts union  # Outputs: 1, 2, 3, 4, 5

All these operators create new arrays and leave the original arrays unchanged.

challenge icon

Challenge

Easy

Read two lines of input, each containing comma-separated integers:

  1. The first array (e.g., 1,2,3,4,5)
  2. The second array (e.g., 3,4,5,6,7)

Perform the following operations and print the results, each on its own line:

  1. Print each element of the concatenation of both arrays (using +)
  2. Print --- as a separator
  3. Print each element of the difference (first array minus second, using -)
  4. Print --- as a separator
  5. Print each element of the intersection (common elements, using &)
  6. Print --- as a separator
  7. Print each element of the union (combined without duplicates, using |)

For example, if the inputs are 1,2,3,4,5 and 3,4,5,6,7, the output should be:

1
2
3
4
5
3
4
5
6
7
---
1
2
---
3
4
5
---
1
2
3
4
5
6
7

If the inputs are 10,20,30 and 30,40,50, the output should be:

10
20
30
30
40
50
---
10
20
---
30
---
10
20
30
40
50

If the inputs are 5,10,15 and 1,2,3, the output should be:

5
10
15
1
2
3
---
5
10
15
---
---
5
10
15
1
2
3

Cheat sheet

Ruby provides special operators for combining, comparing, and manipulating arrays:

The + operator concatenates two arrays:

a = [1, 2, 3]
b = [4, 5, 6]
combined = a + b  # [1, 2, 3, 4, 5, 6]

The - operator removes elements from the first array that appear in the second:

all_numbers = [1, 2, 3, 4, 5]
to_remove = [2, 4]
result = all_numbers - to_remove  # [1, 3, 5]

The & operator returns the intersection (common elements):

a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
common = a & b  # [3, 4]

The | operator returns the union (combined without duplicates):

a = [1, 2, 3]
b = [3, 4, 5]
union = a | b  # [1, 2, 3, 4, 5]

All these operators create new arrays and leave the original arrays unchanged.

Try it yourself

# Read input arrays
input1 = gets.chomp.split(',').map(&:to_i)
input2 = gets.chomp.split(',').map(&:to_i)

# TODO: Write your code below
# Perform the following operations:
# 1. Concatenation using +
# 2. Difference using -
# 3. Intersection using &
# 4. Union using |
# Print each result with "---" as separator between operations
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals