-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathIntegerDuplicateDeleter.java
More file actions
76 lines (54 loc) · 1.75 KB
/
IntegerDuplicateDeleter.java
File metadata and controls
76 lines (54 loc) · 1.75 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.zipcodewilmington.looplabs;
import java.lang.reflect.Array;
/**
* Created by leon on 1/29/18.
* @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
*/
public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
private Integer[] result;
public IntegerDuplicateDeleter(Integer[] intArray) {
super(intArray);
}
@Override
public Integer[] removeDuplicates(int maxNumberOfDuplications) {
int duplicates = 0;
int indexNewAray = 0;
for(Integer index : array){
if(countOccurences(index) >= maxNumberOfDuplications)
duplicates++;
}
result = new Integer[array.length - duplicates];
for(Integer index : array){
if(countOccurences(index) < maxNumberOfDuplications) {
result[indexNewAray] = index;
indexNewAray++;
}
}
return result;
}
@Override
public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
int duplicates = 0;
int indexNewAray = 0;
for(Integer index : array){
if(countOccurences(index) == exactNumberOfDuplications)
duplicates++;
}
result = new Integer[array.length - duplicates];
for(Integer index : array){
if(countOccurences(index) != exactNumberOfDuplications) {
result[indexNewAray] = index;
indexNewAray++;
}
}
return result;
}
public int countOccurences(int index){
int counter = 0;
for(Integer number : array) {
if (index == number)
counter++;
}
return counter;
}
}