Write a program to read two integer arrays and find the sum of common elements in both the arrays. If there are no common elements return -1 as output.
The return type (integer) should be the sum of common elements.
Assume that all the elements will be distinct.
Input and Output Format
- Input consists of 2n+1 integers. The first integer input corresponds to n, the number of elements in the array. The next ‘n’ integers correspond to the elements in the array, The last n elements correspond to the elements of the second array.
- Output consists of a single integer value.
Note: Assume that the maximum number of elements in the array is 20.
Refer sample output for formatting specifications
Sample Input 1:
4
1
2
3
4
2
3
6
7
Sample Output 1:
5
Find the sum of the common element in the array in Java
Following are the steps to find the sum of common elements:
- Input the size of an array. Create two arrays of size ‘n’.
- Add elements to both the array. Pass them to display() method.
- Inside the method, declare and initialize the variable sum to 0.
- Now, use for loop to iterate over both the array and compare elements of both the array. If they are the same, then add them to the sum variable.
- At last, return the value in sum.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++)
b[i] = Integer.parseInt(sc.nextLine());
System.out.println(display(a, b));
}
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
Sum of Common elements using ArrayList
Following are the steps to find the sum of common elements using ArrayList:
- Input size of an array. Create two empty array lists.
- Add elements to both lists. Pass these lists to display() method.
- Inside the method, use the retainAll() method to find common elements in both lists.
- Now, the listOne will contain the common elements. Just iterate over the listOne using for each loop and add it to the sum variable.
- At last, just print the value of the sum.
package com.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class TestJava {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> listOne = new ArrayList<>();
for(int i = 0 ;i<n;i++){
listOne.add(sc.nextInt());
}
List<Integer> listTwo = new ArrayList<>();
for(int i = 0 ;i<n;i++){
listTwo.add(sc.nextInt());
}
display(listOne,listTwo);
}
private static void display(List<Integer> listOne, List<Integer> listTwo) {
listOne.retainAll(listTwo);
int sum = 0;
System.out.println("Common Elements "+listOne);
for ( Integer list1:listOne) {
sum = sum + list1;
}
System.out.println("Sum is "+sum);
}
}
Output
Sum of Common elements using Java 8 Stream filter
Following are the steps to find the sum of common elements using a stream filter:
- Input size of an array. Create two empty array lists.
- Add elements to both lists. Pass these lists to display() method.
- Inside the method, use the following stream filter to find common elements in both lists.
List<Integer> result = listOne.stream()
.filter(listTwo::contains)
.collect(Collectors
.toList());
- Now, the result will contain the common elements. Just iterate over the result list using for each loop and add it to the sum variable.
- At last, just print the value of the sum.
package com.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class TestJava {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> listOne = new ArrayList<>();
for(int i = 0 ;i<n;i++){
listOne.add(sc.nextInt());
}
List<Integer> listTwo = new ArrayList<>();
for(int i = 0 ;i<n;i++){
listTwo.add(sc.nextInt());
}
display(listOne,listTwo);
}
private static void display(List<Integer> listOne, List<Integer> listTwo) {
List<Integer> result = listOne.stream()
.filter(listTwo::contains)
.collect(Collectors
.toList());
int sum = 0;
System.out.println("Common Elements "+result);
for ( Integer list1:result) {
sum = sum + list1;
}
System.out.println("Sum is "+sum);
}
}
Output
Sum of Common elements using Java 8 distinct()
Following are the steps to find the sum of common elements using a distance() method:
- Input size of an array. Create two empty array lists.
- Add elements to both lists. Pass these lists to display() method.
- Inside the method, use the following distinct() method to find common elements in both lists.
List<Integer> result = listOne.stream().distinct()
.filter(listTwo::contains)
.collect(Collectors
.toList());
- Now, the result will contain the common elements. Just iterate over the result list using for each loop and add it to the sum variable.
- At last, just print the value of the sum.
package com.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class TestJava {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> listOne = new ArrayList<>();
for(int i = 0 ;i<n;i++){
listOne.add(sc.nextInt());
}
List<Integer> listTwo = new ArrayList<>();
for(int i = 0 ;i<n;i++){
listTwo.add(sc.nextInt());
}
display(listOne,listTwo);
}
private static void display(List<Integer> listOne, List<Integer> listTwo) {
List<Integer> result = listOne.stream().distinct()
.filter(listTwo::contains)
.collect(Collectors
.toList());
int sum = 0;
System.out.println("Common Elements "+result);
for ( Integer list1:result) {
sum = sum + list1;
}
System.out.println("Sum is "+sum);
}
}
Output
Thus, in this way, we learn how to find the sum of common elements from an array.