Ruby – Set Operations

The set is an unordered collection with no duplicate items. Like other languages, Ruby also provides a set class that represents the mathematical notion of a set. Here, we will discuss the basic set operations, i.e., Union, Intersection, and Difference.

Union

In Ruby, the union takes two objects like arrays, vectors, hashes, etc. as arguments and results in a third object which contains all the elements from both the objects and removes duplicate elements. You can perform a union between two arrays using the pipe operator(|).

Syntax:
x | y

Parameters: x and y are the sequence items of objects.

For Example:



[1, 2, 1, 2, 3, 5, 7] | [1, 2, 3, 4] #=> [1, 2, 3, 4, 5, 7]


Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate the union
  
# Array
arr_1 = [ 2, 4, 5, 6, 7, 9, 1, 2, 4
arr_2 = [ 2, 4, 5, 6, 9, 8, 0, 3, 3
  
# Union 
 result = arr_1 | arr_2
   
 # Display result
 puts "#{result}"

chevron_right


Output:

[2, 4, 5, 6, 7, 9, 1, 8, 0, 3]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate Union of arrays
   
class MultiSet
  attr_accessor :set
  def initialize(set)
    @set = set
  end
  
# Union
  def |(other)
    @set | other.set
  end
end
   
x = MultiSet.new([ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8 ])
y = MultiSet.new([ 1, 3, 5, 6, 8 ])
   
p x | y 

chevron_right


Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Intersection

In Ruby, intersection takes two objects like arrays, vectors, hashes, etc. as arguments and results in a third object which contains elements that are common in both the objects and remove duplicates. The order in the intersection is preserved from the original array. You can perform intersection either by using & operator or by using the intersection() function.

Syntax:
x & y

Parameters: x and y are the sequence items of objects.

For Example:



[ 'a', 'b', 'b', 'z' ] & [ 'a', 'b', 'c' ,'z']   #=> [ 'a', 'b' ,'z']


Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate the intersection method 
  
# requires the set 
require "set"
  
x = Set.new([ 1, 2, 3, 4 ])
y = Set.new([ 1, 2, 4 ])
  
# Intersection method used 
res = x.intersection(y) 
  
# Prints the res 
puts res

chevron_right


Output:

Set: {1, 2, 4}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate intersection of arrays
class MultiSet
  attr_accessor :set
  def initialize(set)
    @set = set
  end
  
# Intersection
  def &(other)
    @set & other.set
  end
      
x = MultiSet.new([ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8 ])
y = MultiSet.new([ 1, 3, 5, 6, 8 ])
   
p x & y 
end

chevron_right


Output:

[1, 3, 5, 6, 8]

Difference

In Ruby, difference takes two objects like arrays, vectors, hashes, etc. as arguments and returns the difference of both the objects. You can perform difference either by using – operator or by using the difference() function.

Syntax:
x – y

Parameters: x and y are the sequence items of objects.

For Example:

[ 22, 45, 89, 76 ] - [ 22, 22, 89, 79 ]   #=> [45]


Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate the difference method 
  
# requires the set 
require "set"
  
x = Set.new([ 1, 2, 3, 4 ])
y = Set.new([ 1, 2, 4, 5 ])
  
# Difference method used 
res = x.difference(y) 
  
# Prints the res 
puts res

chevron_right


Output:

Set: {3}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate difference of arrays
class MultiSet
  attr_accessor :set
  def initialize(set)
    @set = set
  end
  
# Difference
  def -(other)
    @set - other.set
  end
  
x = MultiSet.new([ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8 ])
y = MultiSet.new([ 1, 3, 5, 6, 8 ])
   
p x - y 
end

chevron_right


Output:

[2, 2, 4, 7]



My Personal Notes arrow_drop_up

Recommended Posts:


    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.