Java

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

Daeryuk Kim 2024. 3. 2. 20:10
import java.util.*;

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

        return answer;
    }
public static void main(String[] args) {
        int[] arr = {1,1,3,3,0,1,1};
        Solution sol = new Solution();
        
        
        int[] result = sol.solution(arr);
        
        // 결과 배열을 출력
        System.out.println(Arrays.toString(result));
    
    }
}