본문 바로가기
Algorithm/PROGRAMMERS[Java]

Lv1. 같은 숫자는 싫어

by 씨니 2022. 4. 17.
728x90

▶ 문제

 

▶ 풀이방법

import java.util.*;

public class Solution {
    public int[] solution(int []arr) {
        int[] answer = {};
        
        int index = 0;
        ArrayList<Integer> list = new ArrayList<>();
        list.add(arr[0]);
        for(int i = 1; i < arr.length; i++){
            if(list.get(index) != arr[i]){
                list.add(arr[i]);
                index++;
            }
        }
        
        answer = new int[list.size()];
        for(int i = 0; i < list.size(); i++) answer[i] = list.get(i);
        
        return answer;
    }
}
728x90