Write a Java program to find Sum of Common Elements in two array

Write a program to find out the sum of common elements in given two arrays. If no common elements are found print – “No common elements”.

Input and Output Format

  • Input consists of 2+m+n integers. The first integer corresponds to m (Size of the 1st array), the second integer corresponds to n (Size of the 2nd array), followed by m+n integers corresponding to the array elements.
  • The output consists of a single integer corresponding to the sum of common elements or a string “No common elements”.

Refer sample output for formatting specifications

Assume the common element appears only once in each array.

Sample Input 1:

4

3

2

3

5

1

1

3

9

Sample Output 1:

4

Sample Input 2:

4

3

2

3

5

1

12

31

9

Sample Output 2:

No common elements

Find Sum of Common Elements in two arrays in Java

Following are the steps to find the sum of common elements in two arrays:

  • Input the size of the arrays from the user.(m & n)
  • Create two arrays of integers of size m and n.
  • Add elements to both the arrays.
  • Now, pass both the arrays to the display() method.
  • Inside the method, declare and initialize the variable sum to 0.
  • Iterate over both the array using for loop and compare elements of the first array with the element of the second array.
  • If both are equal, then take the sum of those elements and transfer it to the sum variable.
  • At last, return the value of the sum.
  • If the sum value is 0, then we will print “no common elements”.
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int[] a = new int[n];
    int[] b = new int[m];
    for (int i = 0; i < n; i++)
      a[i] = sc.nextInt();
    for (int i = 0; i < m; i++)
      b[i] = sc.nextInt();
    int u = display(a, b);
    if (u == -1)
      System.out.println("No common elements");
    else
      System.out.println(u);
  }
  
  // Method...................
  public static int display(int a[], int b[]) {

    int sum = 0;
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < b.length; j++) {
        if (a[i] == b[j])
          sum = sum + a[i];
      }
    }
    if (sum == 0)
      return -1;
    else
      return sum;
  }

}

Output

Find Common Elements from two arrays

Write a code to find the common elements from the two given arrays.

Input and Output Format

  • Input consists of 2+m+n integers. The first integer corresponds to m (Size of the 1st array), the second integer corresponds to n (Size of the 2nd array), followed by m+n integers corresponding to the array elements.
  • The output consists of common elements from both arrays.

Sample Input:

3

3

1

2

3

2

1

5

Sample Output

1

2

Program to find common elements from the two arrays

Following are the steps to find the common elements in two arrays:

  • Input the size of the arrays from the user.(m & n)
  • Create two arrays of integers of size m and n.
  • Add elements to both the arrays.
  • Now, pass both the arrays to the display() method.
  • Iterate over both the array using for loop and compare elements of the first array with the element of the second array.
  • If both are equal, then print the common elements.
package com.demo3;

import java.util.Scanner;

public class CommonElements {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[m];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 0; i < m; i++) b[i] = sc.nextInt();
         display(a, b);
    }

    public static void display(int a[], int b[]) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if(a[i]==b[j])
                {
                    System.out.println("Common Elements "+a[i]);
                }
            }
        }

    }
}

Output

Thus, in this way, we learn how to find the common elements from two arrays.