문제
코드
- 첫 숫자를 result에 넣는다.
- 앞 숫자와 비교
- 다르다면 새로운 숫자이므로 result에 삽입하고
- 같다면 중복되는 숫자이므로 넘어간다.
/** 문제 설명
arr[] = 0 ~ 9 종류의 숫자로 이루어져 있음
연속되는 숫자는 하나만 남기고 나머지 삭제하고 배열 반환(이때 원소 순서는 유지)
*/
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
result.add(arr[i]);
continue;
}
if (arr[i] != arr[i-1])
result.add(arr[i]);
}
return result.stream()
.mapToInt(i -> i)
.toArray();
}
}
'코딩 테스트 > 자료구조' 카테고리의 다른 글
[프로그래머스] 42587 프로세스(Lv2) - 큐(우선순위 큐) (0) | 2024.06.29 |
---|---|
[프로그래머스] 42586 기능개발(Lv2) - 배열(스택, 큐) (0) | 2024.06.28 |
[백준] 1374 강의실(골드5) - 힙(Heap) (0) | 2024.06.28 |
[백준] 11286 절댓값 힙(실버1) - 힙(Heap), 우선순위 큐(PriorityQueue) (0) | 2024.06.27 |
[프로그래머스] 이중우선순위큐(Lv3) - 힙(Heap), 우선순위 큐(PriorityQueue) (0) | 2024.06.27 |