|
@@ -0,0 +1,62 @@
|
|
1
|
+package com.zipcodewilmington.looplabs;
|
|
2
|
+
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+
|
|
6
|
+public class ArrayUtils<T> {
|
|
7
|
+
|
|
8
|
+ private T[] content;
|
|
9
|
+
|
|
10
|
+ public ArrayUtils(T[] content) {
|
|
11
|
+ this.content = content;
|
|
12
|
+ }
|
|
13
|
+
|
|
14
|
+ public T[] removeDuplicates(T[] arr, T value) {
|
|
15
|
+ int countOfValue = countValues(arr, value);
|
|
16
|
+ int index = 0;
|
|
17
|
+ T[] arr2 = (T[]) Array.newInstance(content.getClass().getComponentType(), arr.length - countOfValue);
|
|
18
|
+ for (T i: arr
|
|
19
|
+ ) {
|
|
20
|
+ if (!i.equals(value)) {
|
|
21
|
+ arr2[index++] = i;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ return arr2;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public int countValues(T[] arr, T value) {
|
|
28
|
+ int countOfValue = 0;
|
|
29
|
+
|
|
30
|
+ for (T t: arr
|
|
31
|
+ ) {
|
|
32
|
+ if (t.equals(value)) {
|
|
33
|
+ countOfValue++;
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ return countOfValue;
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ public T[] removeDupes(T[] arr, int maxNumberOfDuplications) {
|
|
40
|
+ T[] arr2 = Arrays.copyOf(arr, arr.length);
|
|
41
|
+ for (T i: arr2) {
|
|
42
|
+ if ((countValues(arr2, i) >= maxNumberOfDuplications)) {
|
|
43
|
+ arr2 = removeDuplicates(arr2, i);
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+ return arr2;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public T[] removeDupesExactly(T[] arr, int exactNumberOfDuplications) {
|
|
50
|
+
|
|
51
|
+ T[] arr2 = Arrays.copyOf(arr, arr.length);
|
|
52
|
+ for (T i: arr2
|
|
53
|
+ ) {
|
|
54
|
+ if ((countValues(arr2, i) == exactNumberOfDuplications)) {
|
|
55
|
+ arr2 = removeDuplicates(arr2, i);
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ return arr2;
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+}
|