Home »
Ruby »
Ruby Programs
Check the presence of an element in the set in Ruby
Checking presence of an element in set: Here, we are going to learn how to check whether an element is present in the set or not in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on October 11, 2019
The question is all about to find out whether an element is present in the specified set or not? We are trying to perform this task with the help of two logics. There are lots of alternatives to a solution but here we are focusing on two alternatives only. Let us see how we find out the solution.
Methods used:
- === : This operator is an alias for .include? method and returns true or false.
- set.each : This method is used to process an individual element from the set.
- set.include?() : This method returns true or false. This is used to check the presence of an element in the set.
Variable used:
- Vegetable : This is an instance of Set class.
- element : It is containing the string which is entered by the user.
Program 1:
=begin
Ruby program to find the presence of an element in a set.
=end
require 'set'
Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
puts "Enter the element:-"
element = gets.chomp
val = Vegetable === element
if val == true
puts "#{element} is Present in the set."
else
puts "#{element} is not Present in the set."
end
Output
RUN 1:
Enter the element:-
tomato
tomato is not Present in the set.
RUN 2:
Enter the element:-
peas
peas is Present in the set.
Explanation:
In the above code, we are taking input from the user and that input is nothing but the element which we want to search. We are taking help from === operator here. With the help of this operator, we don't need to employ any kind of loop and process individual elements. Our tasks get done in a few lines of code with the help of === operator.
Program 2:
=begin
Ruby program to find the presence of an element in a set.
=end
require 'set'
Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
puts "Enter the element:-"
element = gets.chomp
val1 = false
Vegetable.each do |string|
if string == element
val1 = true
end
end
if val1 == true
puts "#{element} is present in the set."
else
puts "#{element} is not present in the set."
end
Output
RUN 1:
Enter the element:-
tomato
tomato is not Present in the set.
RUN 2:
Enter the element:-
peas
peas is Present in the set.
Explanation:
In the above code, we are testing each element from the set and we are going to check whether it matches the element entered by the user. If it is matched with the element, the flag is set to be true. Later if the flag is found true, the string is printed on the console which informs about the presence of element and if the element is not present, a message is printed on the console which tells the element is not available.
Program 3:
=begin
Ruby program to find the presence of an element in a set.
=end
require 'set'
Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
puts "Enter the element:-"
element = gets.chomp
if Vegetable.include?(element)
puts "#{element} is present"
else
puts "#{element} is not present"
end
Output
RUN 1:
Enter the element:-
peas
peas is present
RUN 2:
Enter the element:-
onion
onion is not present
Explanation:
In the above code, we are employing set.include? method to check the presence of the element which is entered by the user. This makes the task pretty simple and makes our code more efficient. With the help of this method, we also reduce the line of code.
Ruby Set Programs »