find common elements between two Arrays

By | December 9, 2018

In this post on How to find common elements between two Arrays. we will understand how to find common elements between two given Arrays. It is a  very common interview question where it is asked with a condition not to use any inbuilt classes or methods or any inbuilt data structure like List or Set.

A very easy approach to find common elements between two input arrays is to loop through one of the arrays in the outer loop and then traverse through the other array in the inner loop and compare the elements of the outer array elements with all the elements of the inner array. If we get the same element then print it and break it from the inner loop. that’s yet.

You may also like –
Get largest and smallest number in the given Array
Find duplicate elements in an Array

Java program with array of Integers

Output:

Java program with array of Strings

Here is the logic to find common elements between two arrays is very similar in the case of an array of Strings. Only thing that differs is how you compare Strings you will have to use the .equals method of String class.

Output:

That’s all about this topic Java Program to find duplicate elements in an Array. If you have any doubts or any suggestions to make please drop a comment.

Leave a Reply

Your email address will not be published.