The source code to find the common elements from two arrays is given below. The given program is compiled and executed successfully.
# Ruby program to find the common
# elements from two arrays
arr1 = ["RAM","KRISHNA","SHIV","GANESH"];
arr2 = ["KRISHNA","RAM"];
arr3 = arr1 & arr2;
print "Common array elements:\n#{arr3}";
Output:
Common array elements:
["RAM", "KRISHNA"]
Explanation:
In the above program, we created 2 arrays arr1, arr2. Then we found the common elements from arrays using the "&" operator and printed the result.
Program/Source Code:
The source code to find the common elements from two arrays is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created 2 arrays arr1, arr2. Then we found the common elements from arrays using the "&" operator and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer