|
@@ -2,57 +2,51 @@ import java.util.*;
|
2
|
2
|
import java.util.List;
|
3
|
3
|
import java.util.Collection;
|
4
|
4
|
|
5
|
|
-
|
6
|
|
-public class IntegerDuplicateDeleter extends DuplicateDeleter{
|
7
|
|
-
|
|
5
|
+public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
|
8
|
6
|
public IntegerDuplicateDeleter(Integer[] array) {
|
9
|
7
|
super(array);
|
10
|
8
|
}
|
11
|
|
-
|
|
9
|
+
|
12
|
10
|
public Integer[] removeDuplicates(int maxNumberOfDuplications) {
|
13
|
|
- List<Integer> list = new ArrayList<>();
|
14
|
|
-
|
15
|
|
- // go thru each element in array
|
16
|
|
- for (Object num : super.array) {
|
17
|
|
-
|
18
|
|
- // count occurence
|
19
|
|
- int occurence = countOccurrence((Integer)num);
|
20
|
|
-
|
21
|
|
- //add to new array if occurrence != exactnumber of duplication
|
22
|
|
- if (occurence < maxNumberOfDuplications)
|
23
|
|
- list.add((Integer)num);
|
24
|
|
-
|
25
|
|
- }
|
26
|
|
- return list.toArray(new Integer[list.size()]);
|
|
11
|
+ Integer[] answer = new Integer[array.length];
|
|
12
|
+ int answerIndex = 0;
|
|
13
|
+ for (int i = 0; i < array.length; i++) {
|
|
14
|
+ Integer occurences = getAppearances(array[i]);
|
|
15
|
+ if(occurences < maxNumberOfDuplications) {
|
|
16
|
+ answer[answerIndex] = array[i];
|
|
17
|
+ answerIndex++;
|
|
18
|
+
|
|
19
|
+ }
|
|
20
|
+ }
|
|
21
|
+ Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
|
|
22
|
+ return realAnswer;
|
27
|
23
|
}
|
28
|
|
-
|
29
|
|
-
|
|
24
|
+
|
30
|
25
|
public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
|
31
|
|
- List<Integer> list = new ArrayList<>();
|
32
|
|
-
|
33
|
|
- // go thru each element in array
|
34
|
|
- for (Object num : super.array) {
|
35
|
|
-
|
36
|
|
- // count occurence
|
37
|
|
- int occurrence = countOccurrence((Integer)num);
|
38
|
|
-
|
39
|
|
- //add to new array if occurrence != exactnumber of duplication
|
40
|
|
- if (occurrence != exactNumberOfDuplications)
|
41
|
|
- list.add((Integer)num);
|
42
|
|
-
|
|
26
|
+ Integer[] answer = new Integer[array.length];
|
|
27
|
+ int answerIndex = 0;
|
|
28
|
+ for (int i = 0; i < array.length; i++) {
|
|
29
|
+ Integer occurences = getAppearances(array[i]);
|
|
30
|
+
|
|
31
|
+ if (occurences < exactNumberOfDuplications || occurences > exactNumberOfDuplications){
|
|
32
|
+ answer[answerIndex] = array[i];
|
|
33
|
+ answerIndex++;
|
|
34
|
+ }
|
43
|
35
|
}
|
44
|
|
- return list.toArray(new Integer[list.size()]);
|
|
36
|
+ Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
|
|
37
|
+ return realAnswer;
|
|
38
|
+
|
45
|
39
|
}
|
46
|
40
|
|
47
|
|
- private int countOccurrence(Integer number) {
|
48
|
|
- List<Integer> list = new ArrayList<>();
|
49
|
|
- int count = 0;
|
50
|
|
-
|
51
|
|
- for (Object i : super.array) {
|
52
|
|
- if (i.equals(number)) { // when == 1
|
53
|
|
- count++;
|
|
41
|
+ private int getAppearances(Integer s) {
|
|
42
|
+
|
|
43
|
+ int appearances = 0;
|
|
44
|
+
|
|
45
|
+ for (Integer element: array) {
|
|
46
|
+ if (element == s) { // when == 1
|
|
47
|
+ appearances++;
|
54
|
48
|
}
|
55
|
49
|
}
|
56
|
|
- return count;
|
|
50
|
+ return appearances;
|
57
|
51
|
}
|
58
|
52
|
}
|