Tasks Details
  
    
    
      
        
           
        
          
        
       
      
        
          
          
          
          
            
              
                
        
        
          
  
    
      
    
      
    
  
                        
 
                      
                        
  
  
  
    
      
    
      
        
          
            
              
              
            
            
              
                
              
              
          
        
      
    
  
                        
  
    
    
      
        
          
        
        
    
       
    
  
          
            
          
        
       
    
   
 
                      
                        
  
  
  
    
      
    
      
        
          
            
              
              
            
            
              
                
              
              
          
        
      
    
  
                        
  
    
    
      
        
          
        
        
    
       
    
  
          
            
          
        
       
    
   
 
                      
                        
  
  
  
    
      
    
      
        
          
            
              
              
            
            
              
                
              
              
          
        
      
    
  
                        
  
    
    
      
        
          
            
        
    
  
  
  
    
       
    
      
      
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
        
        
          
            
              
              
            
             
              
             
           
        
      
       
    
       
    
  
          
            
          
        
       
    
   
 
                      
                    
                  
                
              
            
          
          
        
      
    
  
    
          easy
        
        
            1.
            
              Distinct
            
          
          
            Compute number of distinct values in an array.
          
        
  
    
    Task Score
    
    
  
  
    
      
        
          
            
        
      
    
  
          
              100%
            
          
  
    
    Correctness
    
    
  
  
    
      
        
          
            
        
      
    
  
          
              100%
            
          
  
    
    Performance
    
    
  
  
    
      
        
          
            
        
      
    
  
        
              100%
            
          Write a function
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns the number of distinct values in array A.
For example, given array A consisting of six elements such that:
A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
 - each element of array A is an integer within the range [−1,000,000..1,000,000].
 
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
              
            
          
            Solution
            
            
              
                
              
            
          
          
          
            
              Programming language used Java 21
            
          
          
            
            
              Time spent on task 1 minutes
              
            
            
          
          
          
            
              
                
                  Notes
                  
                    
                  
                
               
              
                
              
              
                
                  
                    
                  
                  
                  
                  
                  
                    
                  
                  
                    
                      
                        
  
  
                      
                        
                          not defined yet
                        
                      
                      
                    
                    
                  
        Code: 06:48:19 UTC,
        
          java,
        
        
          autosave 
        
      
      
      
      
    
        Code: 06:48:25 UTC,
        
          java,
        
        
          verify,
          
            
              result: Passed
            
          
          
        
      
      
      
      
    // you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
    public int solution(int[] A) {
		int N = A.length;
		if(0 == N) return 0;
		quickSort(A, 0, N-1);
		int distinctNum = 1;
		for (int i=1; i<N; i++){
			if(0 != (A[i -1] ^ A[i])) distinctNum++;
		}
		return distinctNum;
	}
	public void quickSort(int[] arr, int begin, int end) {
		int middle = (begin + end) / 2;
		int pivot = arr[middle];
		int left = begin;
		int right = end;
		int temp;
		while (left < right) { //left와 right가 만나면 루프 종료
			while (arr[left] < pivot) left++;       //left : pivot 값 보다 큰 값을 찾기 위해 이동
			while (arr[right] > pivot) right--;     //right : pivot 값 보다 작은 값을 찾기 위해 이동
			if (left <= right) { //left가 right보다 같거나 작으면 서로 값 교환해줌
				temp = arr[left];
				arr[left] = arr[right];
				arr[right] = temp;
				left++;
				right--;
			}
		}
		//부분 분할 실행
		if (begin < right) quickSort(arr, begin, right);
		if (end > left) quickSort(arr, left, end);
	}
}
            
          Analysis 
          
            
          
        
        
          
  
  
  
        Code: 06:48:35 UTC,
        
          java,
        
        
          verify,
          
            
              result: Passed
            
          
          
        
      
      
      
      
    // you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
    public int solution(int[] A) {
		int N = A.length;
		if(0 == N) return 0;
		quickSort(A, 0, N-1);
		int distinctNum = 1;
		for (int i=1; i<N; i++){
			if(0 != (A[i -1] ^ A[i])) distinctNum++;
		}
		return distinctNum;
	}
	public void quickSort(int[] arr, int begin, int end) {
		int middle = (begin + end) / 2;
		int pivot = arr[middle];
		int left = begin;
		int right = end;
		int temp;
		while (left < right) { //left와 right가 만나면 루프 종료
			while (arr[left] < pivot) left++;       //left : pivot 값 보다 큰 값을 찾기 위해 이동
			while (arr[right] > pivot) right--;     //right : pivot 값 보다 작은 값을 찾기 위해 이동
			if (left <= right) { //left가 right보다 같거나 작으면 서로 값 교환해줌
				temp = arr[left];
				arr[left] = arr[right];
				arr[right] = temp;
				left++;
				right--;
			}
		}
		//부분 분할 실행
		if (begin < right) quickSort(arr, begin, right);
		if (end > left) quickSort(arr, left, end);
	}
}
            
          Analysis 
          
            
          
        
        
          
  
  
  
        Code: 06:48:41 UTC,
        
          java,
        
        
          final,
          
            score: 
              
                100
              
              
          
          
        
      
      
      
      
    // you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
    public int solution(int[] A) {
		int N = A.length;
		if(0 == N) return 0;
		quickSort(A, 0, N-1);
		int distinctNum = 1;
		for (int i=1; i<N; i++){
			if(0 != (A[i -1] ^ A[i])) distinctNum++;
		}
		return distinctNum;
	}
	public void quickSort(int[] arr, int begin, int end) {
		int middle = (begin + end) / 2;
		int pivot = arr[middle];
		int left = begin;
		int right = end;
		int temp;
		while (left < right) { //left와 right가 만나면 루프 종료
			while (arr[left] < pivot) left++;       //left : pivot 값 보다 큰 값을 찾기 위해 이동
			while (arr[right] > pivot) right--;     //right : pivot 값 보다 작은 값을 찾기 위해 이동
			if (left <= right) { //left가 right보다 같거나 작으면 서로 값 교환해줌
				temp = arr[left];
				arr[left] = arr[right];
				arr[right] = temp;
				left++;
				right--;
			}
		}
		//부분 분할 실행
		if (begin < right) quickSort(arr, begin, right);
		if (end > left) quickSort(arr, left, end);
	}
}
            Analysis summary
            
  The solution obtained perfect score.
          Analysis 
          
            
          
        
        
          
  
    
      Detected time complexity:
        
          O(N*log(N)) or O(N)
        
        
      
        
          expand all 
        
        Correctness tests
      
      
        
        
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                2.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK
                
                
              
            
                1.
              
              
              
                
                  0.004 s
                
              
              
              
                
                  OK