-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution18.java
More file actions
31 lines (25 loc) · 1.12 KB
/
Solution18.java
File metadata and controls
31 lines (25 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* 코딩 테스트 공부 - 18
* 배열 arr가 주어집니다. 배열 arr의 각 원소는 숫자 0부터 9까지로 이루어져 있습니다.
* 이때, 배열 arr에서 연속적으로 나타나는 숫자는 하나만 남기고 전부 제거하려고 합니다.
* 단, 제거된 후 남은 수들을 반환할 때는 배열 arr의 원소들의 순서를 유지해야 합니다.
* 배열 arr에서 연속적으로 나타나는 숫자는 제거하고 남은 수들을 return 하는 solution 함수를 완성해 주세요.
*/
package codingTest;
import java.util.ArrayList;
public class Solution18 {
public int[] solution(int []arr) {
int[] answer = {};
ArrayList<Integer> copyList = new ArrayList<Integer>();
copyList.add(arr[0]);
for(int i = 1; i < arr.length; i++){
if(arr[i] != arr[i-1]){
copyList.add(arr[i]);
}
}
answer = new int[copyList.size()];
for(int i = 0; i < copyList.size(); i++){
answer[i] = copyList.get(i);
}
return answer;
}
}