-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
48 lines (39 loc) · 1.53 KB
/
Copy pathApp.java
File metadata and controls
48 lines (39 loc) · 1.53 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* This Java source file was generated by the Gradle 'init' task.
*/
/*
### 1. 이해
- 배열 arr이 주어짐
- arr은 0 ~ 9의 숫자로 이루어져 있음
- arr에서 연속적으로 나타나는 숫자는 하나만 남기고 전부 제거해야 한다.
- 제거 후, 배열을 반환할 때 순서가 유지되어야 한다.
- arr = [1, 1, 3, 3, 0, 1, 1] 이면 [1, 3, 0, 1] 을 return
- arr = [4, 4, 4, 3, 3] 이면 [4, 3] 을 return
- 배열 arr의 크기 : 1,000,000 이하의 자연수
### 2. 계획
- 재귀함수를 이용하여 배열 원소 1개씩 다른 곳에 넣는다.
- 새로운 배열의 last와 추가할 원소가 같은지 다른지를 체크하여 추가한다.
### 3. 실행
### 4. 반성
Array와 List 그리고 ArrayList에 대해 모르는 것이 많다.
공식문서 또는 도서를 읽어야겠다.
*/
package find.prime.numbers;
import java.util.*;
public class App {
public int[] solution(int []arr) {
ArrayList<Integer> filterednumberList = new ArrayList<Integer>();
filterednumberList.add(arr[0]);
for (Integer number : arr) {
int lastNumber = filterednumberList.get(filterednumberList.size() - 1);
if (lastNumber != number) {
filterednumberList.add(number);
}
}
return filterednumberList.stream().filter(i -> i != null).mapToInt(i -> i).toArray();
}
public static void main(String[] args) {
int[] inputArray = {1, 1, 3, 3, 0, 1, 1};
System.out.println(new App().solution(inputArray));
}
}