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 | import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class Toast {
public static <E> E[] removeDuplicates(E[] array, Comparator<E> comparator){
if(array.length < 2)
return array;
@SuppressWarnings("unchecked")
E[] out = (E[]) Array.newInstance(array.getClass().getComponentType(), array.length);
int max = 0;
out[max++] = array[0];
loop1:
for (int i = 1; i < array.length; i++) {
E t = array[i];
for (int j = 0; j < max; j++) {
if(comparator.compare(t, out[j]) == 0)
continue loop1;
}
out[max++] = t;
}
return Arrays.copyOfRange(out, 0, max);
}
}
|
No comments:
Post a Comment