본문 바로가기

코딩테스트/codility

Distinct

https://app.codility.com/programmers/lessons/6-sorting/distinct/

 

Distinct coding task - Learn to Code - Codility

Compute number of distinct values in an array.

app.codility.com

 

중복되지 않은 숫자의 갯수를 구하면 됨

 

중복을 허용하지 않는 set이 있으므로, 그냥 사용함.

 

import java.util.*;

class Solution {
    public int solution(int[] A) {
        Set<Integer> set = new HashSet();
        for (int a : A) set.add(a);

        return set.size();
    }
}

 

굳이 set을 사용하지 않는다면.. boolean[] 배열로 체크하면 되지 않을까..?

'코딩테스트 > codility' 카테고리의 다른 글

MaxProductOfThree  (0) 2023.09.29
PassingCars  (0) 2023.09.29
FrogRiverOne  (0) 2023.09.29
PermCheck  (0) 2023.09.29
TapeEquilibrium  (0) 2023.09.18