알고리즘/수학문제

[프로그래머스] 없는 숫자 더하기 #Java

VIPeveloper 2022. 5. 20. 15:25
728x90
반응형
import java.io.IOException;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        int [] arr = {1,2,3,4,6,7,8,0};

        System.out.println(solution(arr));
        System.out.println(solution2(arr));
    }
    public static int solution(int[] numbers) {
        int answer = 0;
        int [] ans = new int[10];
        for (int i = 0; i < numbers.length; i++) {
            ans[numbers[i]]++;
        }
        for (int i = 0; i < ans.length; i++) {
            if(ans[i]==0) answer+=i;
        }
        return answer;
    }
    public static int solution2(int[] numbers){
        int total = 45;
        int sum = Arrays.stream(numbers).sum();
        return total - sum;
    }
}
728x90
반응형