-
Notifications
You must be signed in to change notification settings - Fork 0
/
P08.java
49 lines (41 loc) · 1.11 KB
/
P08.java
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
49
package lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* Class for compress duplicates of list elements.
* If a list contains repeated items, it should be replaced with single copy
* of the element.
* <pre>
* compress([a,a,a,a,b,c,c,a,a,d,e,e,e,e]).
* result = [a,b,c,a,d,e]
* </pre>
*/
public final class P08 {
private P08() {
}
/**
* Compress repeated items in list.
*
* @param inputList list of item
* @param <T> type of item in list
* @return compressed list of items
*/
public static <T> List<T> compress(final List<T> inputList) {
Objects.requireNonNull(inputList, "List must be not null");
List<T> result = new ArrayList<>();
if (inputList.isEmpty()) {
return Collections.emptyList();
}
T first = inputList.get(0);
result.add(first);
for (T act : inputList) {
if (!act.equals(first)) {
first = act;
result.add(act);
}
}
return result;
}
}