Write a program to read an integer array, Remove the duplicate elements and display sum of even numbers in the output. If the input array contains only odd number then return -1.
The return type is an integer.
Input and Output Format:
Input consists of an integer n which is the number of elements followed by n integer values.
The output consists of an integer.
Refer sample output for formatting specifications.
Sample Input 1:
7
2
3
54
1
6
7
7
Sample Output 1:
62
Sample Input 2:
6
3
7
9
13
17
21
Sample Output 2:
-1
java program to remove duplicate elements in array
import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println(display(a)); } public static int display(int a[]) { LinkedHashSet<Integer>h1=new LinkedHashSet<Integer>(); int s=0; for(int i=0;i<a.length;i++) { h1.add(a[i]); } Iterator<Integer> it=h1.iterator(); while(it.hasNext()) { int k=it.next(); if(k%2==0) { s=s+k; } } if(s>0) return s; else return -1; } }