forked from Mickey0521/Codility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinct.java
More file actions
30 lines (22 loc) · 695 Bytes
/
Distinct.java
File metadata and controls
30 lines (22 loc) · 695 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
25
26
27
28
29
30
package Distinct;
// note: remember to import (for using "Arrays.sort(xxx[])")
import java.util.*;
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// special case
if(A.length ==0)
return 0;
// initial setting: one distinct number
int result =1;
// Using "Arrays.sort(A)" (important)
Arrays.sort(A);
// for counting the distinct numbers
for(int i=1; i < A.length; i++){
if(A[i] != A[i-1]){ // distinct
result++;
}
}
return result; // return the number of distinct values
}
}