-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathStringDuplicateDeleter.java
More file actions
75 lines (53 loc) · 1.66 KB
/
StringDuplicateDeleter.java
File metadata and controls
75 lines (53 loc) · 1.66 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
package com.zipcodewilmington.looplabs;
/**
* Created by leon on 1/28/18.
* @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
*/
public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
private String[] result;
public StringDuplicateDeleter(String[] intArray) {
super(intArray);
}
@Override
public String[] removeDuplicates(int maxNumberOfDuplications) {
int duplicates = 0;
int indexNewAray = 0;
for(String s : array){
if(countOccurences(s) >= maxNumberOfDuplications)
duplicates++;
}
result = new String[array.length - duplicates];
for(String s: array){
if(countOccurences(s) < maxNumberOfDuplications) {
result[indexNewAray] = s;
indexNewAray++;
}
}
return result;
}
@Override
public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
int duplicates = 0;
int indexNewAray = 0;
for(String s : array){
if(countOccurences(s) == exactNumberOfDuplications)
duplicates++;
}
result = new String[array.length - duplicates];
for(String s: array){
if(countOccurences(s) != exactNumberOfDuplications) {
result[indexNewAray] = s;
indexNewAray++;
}
}
return result;
}
public int countOccurences(String string){
int counter = 0;
for(String s : array) {
if (s.equals(string))
counter++;
}
return counter;
}
}