less than 1 minute read

[Lesson6] Sorting

Distinct : Compute number of distinct values in an array.


Java Solution

import java.util.*;

class Solution {
    public int solution(int[] A) {
        HashSet<Integer> distinct = new HashSet<Integer>();
        for(int i = 0; i < A.length; i++){
            distinct.add(A[i]);
        }
        int result = distinct.size();
        return result;
    }
}

PHP Solution

function solution($A) {
    $distinct = array();
    foreach($A as $value){
        if(!isset($distinct[$value])){
            $distinct[$value] = true;
        }else{
            unset($distinct[$value]);    
        }
    }
    return key($distinct);
}