공부기록/프로그래머스

[프로그래머스] 같은 숫자는 싫어

메델 2023. 9. 14. 04:20
import java.util.*;

public class Solution {
    public ArrayList<Integer> solution(int []arr) {
        ArrayList<Integer> list = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        stack.push(arr[0]);
        
        for(int i=1; i<arr.length; i++){
            if(arr[i-1] != arr[i]){
                stack.push(arr[i]);
            }
        }
        
        for(int i=0; i<stack.size(); i++){
            list.add(stack.get(i));
        }

        return list;
    }
}