-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctCount.java
More file actions
24 lines (23 loc) · 816 Bytes
/
DistinctCount.java
File metadata and controls
24 lines (23 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Arrays;
import java.util.Scanner;
public class DistinctCount {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter the number of elements you want in array :");
int size=sc.nextInt();
int[] arr =new int[size];
System.out.print("Enter the elements of array one with spaces:");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int distinctSum =0;
for(int i = 1; i<size; i++){
if(arr[i] != arr[i-1]){
distinctSum = distinctSum + arr[i-1];
}
}
System.out.printf("The Sum of Distinct elements in array is \"%d\"",distinctSum+arr[size-1]);
sc.close();
}
}