Java Program to Find the Common Strings in Two String Arrays

In the previous article, we have seen Java Program to Find the Common Elements between Two Integer Arrays

In this article we are going to find common strings between two string arrays in Java.

Java Program to Find the Common Strings in Two String Arrays

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Now let’s see the solution to the problem.

Approach:

  • Create two arrays.
  • Display both of them to the user.
  • Use two for loops to iterate both arrays.
  • Print common elements between them.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Crating an array
        String arr1[] = {"cat", "dog", "mouse"};
        String arr2[] = {"elephant", "cat", "mouse", "lion", "zebra"};
        // Displaying the array
        System.out.print("Array 1 : ");
        printArray(arr1);
        System.out.print("Array 2 : ");
        printArray(arr2);
        System.out.print("The common elements are : ");
        // Print common elements
        printCommon(arr1,arr2);
    }

    // Function to print the array
    static void printArray(String arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    
    public static void printCommon(String arr1[], String arr2[])
    {  
        // Checks for common elements
        for (int i=0; i<arr1.length; i++){
            for(int j=0;j<arr2.length;j++)
                if (arr1[i].equals(arr2[j])){
                    System.out.print(arr1[i]+" "); 
            }  
        }
    }  
}

Output:

Array 1 : cat dog mouse 
Array 2 : elephant cat mouse lion zebra 
The common elements are : cat mouse

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: