-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_03_ArrayList.java
More file actions
33 lines (27 loc) · 986 Bytes
/
Problem_03_ArrayList.java
File metadata and controls
33 lines (27 loc) · 986 Bytes
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
import java.util.*;
//import java.util.ArrayList;
public class Problem_03_ArrayList {
public static int[] solution(int[] arr) {
ArrayList<Integer> answer = new ArrayList<>();
for (int num : arr) {
if (answer.isEmpty() || answer.get(answer.size() - 1) != num) {
answer.add(num);
}
}
int[] intArray = new int[answer.size()];
for (int i = 0; i < answer.size(); i++) {
intArray[i] = answer.get(i);
}
return intArray;
}
public static void main(String[] args) {
int[] arr1 = {1,1,3,3,0,1,1};
int[] arr2 = {4,4,4,3,3};
int[] answer1 = {};
int[] answer2 = {};
answer1 = Problem_03_ArrayList.solution(arr1);
answer2 = Problem_03_ArrayList.solution(arr2);
System.out.println("answer1 = " + Arrays.toString(answer1));// 1, 3, 0, 1
System.out.println("answer2 = " + Arrays.toString(answer2));// 4, 3
}
}