Q:

Java program to find the common elements in two integer arrays

belongs to collection: Java Array Programs

0

Given two integer arrays and we have to find common integers (elements) using java program.

Example:

    Input:
    Array 1 elements: 1993, 1995, 2000, 2006, 2017, 2020
    Array 2 elements: 1990, 1993, 1995, 2010, 2016, 2017
    Output:
    Common elements: 1993, 1995, 2017

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find common elements in two array elements in java

// This program will find common elements from the integer array.

import java.util.Arrays;

public class ExArrayCommonInteger
{
	public static void main(String[] args) 
	{
		// take default integer value.
		int[] array1 = {1993,1995,2000,2006,2017,2020};
		int[] array2 = {1990,1993,1995,2010,2016,2017};

		// print the entered value.
		System.out.println("Array1 : "+Arrays.toString(array1));
		System.out.println("Array2 : "+Arrays.toString(array2));


		for (int i = 0; i < array1.length; i++)
		{
			for (int j = 0; j < array2.length; j++)
			{
				if(array1[i] == (array2[j]))
				{
					// return common integer value.
					System.out.println("Common element is : "+(array1[i]));
				}
			}
		}
	}
}

Output

    Array1 : [1993, 1995, 2000, 2006, 2017, 2020]
    Array2 : [1990, 1993, 1995, 2010, 2016, 2017]
    Common element is : 1993
    Common element is : 1995
    Common element is : 2017

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find the common strings in two str... >>
<< Java program to check sparse matrix...