Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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 31
Archives
Today
Total
관리 메뉴

KSI일기장

프로그래머스 배열 유사도(자바) 본문

JavaAlgorithm

프로그래머스 배열 유사도(자바)

MyDiaryYo 2023. 8. 7. 19:15

두 배열이 얼마나 유사한지 확인하기

배열 s1,s2의 원소를 비교해 같은 원소의 개수를 구하시오.

 

방법1.

 public int solution(String[] s1, String[] s2) {
        int answer = 0;
        
        for(int i=0; i<s1.length; i++){
            for(int j=0; j<s2.length; j++){
                if(s1[i].equals(s2[j])){
                    answer++;
                }
            }
        }
        
        return answer;
    }

 

방법2.

 public int solution(String[] s1, String[] s2) {
        Set<String> set = new HashSet<>(Arrays.asList(s1));
        return (int)Arrays.stream(s2).filter(set::contains).count();
    }