# Ruby Set intersect? Method with Example

**Ruby Set intersect? Method**: Here, we are going to learn about the **intersect? Method of Set class with example in Ruby programming language**.

Submitted by Hrithik Chandra Prasad, on November 02, 2019

## Ruby Set intersect? Method

**intersect?(Set) method** is a method which is predefined in Ruby's library. You may call this method as an exact opposite of Set.disjoint?() method. With the help of this method, we can check whether the two sets are having any common elements. If the sets which we are comparing are having any of the common elements then the method will return true and false in the case only when there are no common elements in both the sets. This method may provide you various advantages while programming with Ruby. Let us see its syntax and example for having a better understanding of how this method is implemented in the Ruby code.

**Syntax:**

Set.intersect?(Set)

**Example 1:**

=begin Ruby program to demonstrate the implementation of intersect?() method. =end require 'set' Vegetable=Set.new(["potato", "brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"]) Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"]) p = Vegetable.intersect?(Fruits) if p == false puts "There is no common element exists between both the sets." else puts "The sets are having common elements." end

**Output**

There is no common element exists between both the sets.

**Explanation:**

In the above code, we have declared two instances of Set class known as Vegetable and Fruits. We want to check whether there exist some common elements between both the sets or not. We are proceeding with the help of the **intersect? method**. We know that it returns a Boolean value. So, we are storing its value inside a variable. We are then checking the value of that variable, if it is false then it simply means that there are no common elements between both the sets. This method will give you result as true even if there is only one element that is common between both the instances of Set class.

**Example 2:**

=begin Ruby program to show the implementation of intersect?() . =end require 'set' p = Set[2,3,5].intersect?Set[2,56,4,3,22,66,34] if p == false puts "There is no common element exists between both the sets." else puts "The sets are having common elements." end

**Output**

The sets are having common elements.

**Explanation:**

In the above code, we are creating sets at the time of invoking the **intersect? function**. The **function intersect?()** is checking whether both the sets are having some common elements or not. If the sets are having even a single common element then it will return true. We are storing its returned value inside a variable ‘p’. We are checking the value of p, if it is having false then it means that there are some common elements inside both the sets. We are informing the user about this with the help of puts statements.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.