Skip to content

Commit 18f9e8c

Browse files
committed
[PGS] 같은 숫자는 싫어 / Level 1 / 15분 / 성공
1 parent 2ac22bc commit 18f9e8c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int[] solution(int []arr) {
5+
ArrayList<Integer> answer = new ArrayList<>();
6+
7+
// 1. 첫 번째 값은 무조건 추가
8+
answer.add(arr[0]);
9+
10+
// 2. 두 번째 값부터 순차적으로 확인
11+
for (int i = 1; i < arr.length; i++) {
12+
// 바로 이전 값과 다르면 추가
13+
if (answer.get(answer.size()-1) != arr[i]) {
14+
answer.add(arr[i]);
15+
}
16+
}
17+
18+
// 3. ArrayList → int[] 변환
19+
int[] answer2 = new int[answer.size()];
20+
for (int i = 0; i < answer.size(); i++){
21+
answer2[i] = answer.get(i);
22+
}
23+
return answer2;
24+
}
25+
}

0 commit comments

Comments
 (0)