Add occurrences of thrice in java

In this challenge you need to check for thrice occurrences in array and sum it up.


Input Format - 
you will be taking a number as an input from STDIN which tells about the length of the array. On another line, array elements should be there with single space between them. 

Output Format 
You need to print the sum of the numbers occurring Thrice. 
/*
* Enter your code here. Read input from STDIN. Print your output to STDOUT.
* Your class should be named CandidateCode.
*/
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int size;
size=sc.nextInt();
int [] arr=new int [size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
int sum=0,val=0;
int count=0;
for(int a=0;a<arr.length;a++){
int x=arr[a];
for(int b=a+1;b<arr.length;b++){
int y=arr[b];
if(x==y){
count++;
val=x; //val to store thrice occurrence value.
}
}
if(count>1){
sum+=val; //sum to add thrice occurrences value in array.
}
count=0; // resetting the counter.
}
System.out.println(sum);
sc.close();
sc.close();
}
}

Sample Test Case: 
Sample Input:-12
9 8 7 7 8 6 5 4 7 8 1 2 

Sample Output 
15

Explanation: 
elements 8 and 7 occurring Thrice and their sum is 15.

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series