×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to find the distinct elements from two arrays

Last Updated : December 15, 2025

Problem Solution

In this program, we will find the distinct elements of two arrays using the "|" operator and print the result.

Program/Source Code

The source code to find the distinct elements from two arrays is given below. The given program is compiled and executed successfully.

# Ruby program to find the distinct elements
# from two arrays

arr1 = ["RAM","KRISHNA","SHIV","GANESH"];
arr2 = ["KRISHNA","RAM", "MOHAN"];

arr3 = arr1 | arr2;

print "Distinct array elements:\n#{arr3}";

Output

Distinct array elements:
["RAM", "KRISHNA", "SHIV", "GANESH", "MOHAN"]

Explanation

In the above program, we created 2 arrays arr1, arr2. Then we found the distinct elements from arrays using the "|" operator and printed the result.

Ruby Arrays Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.