ADVERTISEMENT
ADVERTISEMENT

Java program to find the common elements in two integer arrays

In this java program, we are going to find and print the common elements from two integer arrays; here we have two integer arrays and printing their common elements, which exist in both of the arrays.
Submitted by IncludeHelp, on November 25, 2017

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

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

Java Array Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.