find common elements between two Arrays

By | December 9, 2018

In this post of How 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 simple solution to find common elements between two given arrays is to loop through one of the array in the outer loop and then traverse through the other array in the inner loop and compare the element of the outer array element with all the elements of the inner array.

If same element is found print it and break from the inner loop.

You may also like –
Find 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 remains same in case of array of Strings. Only thing that changes is how you compare, with Strings you will have to use .equals method.

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. Required fields are marked *