×

Java Programs

Java Practice

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. By IncludeHelp Last updated : December 23, 2023

Problem statement

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

Java program to find the common elements in two integer arrays

// 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 »

More Java Array Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.