-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathStringDuplicateDeleter.java
More file actions
154 lines (118 loc) · 4.88 KB
/
StringDuplicateDeleter.java
File metadata and controls
154 lines (118 loc) · 4.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.zipcodewilmington.looplabs;
import java.util.Arrays;
/**
* 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> {
//constructor
public StringDuplicateDeleter (String[] intArray){
super(intArray);
}
String[] anIntArray = Arrays.copyOf(this.array,this.array.length);
public String[] removeDuplicates(int maxNumberOfDuplications) {
//create copy of array, this will be your new array
//read array
//getNumberOfOccurences
//if getNumberOfOccurences appears more than once
//removeValue
//else
//store in newArray
//return values
/*private static final NULL_ARRAY = new String[0];
else if (anIntArray == null){
return NULL_ARRAY;
}*/
/*okay first we are making a new array, but a copy, it will copy this.array which is intArray and copy
its length (it takes two parameters)*/
int noDuplicates = 0;
String[] duplicates = new String[0];
int duplicatesIndex = 0;
for (int i = 0; i < anIntArray.length; i++) {
if (contains(duplicates, anIntArray[i])) {
continue;
} else if (getNumberOfOccurrences(anIntArray, anIntArray[i]) >= maxNumberOfDuplications) {
duplicates = Arrays.copyOf(duplicates, duplicates.length + 1);
duplicates[duplicatesIndex] = array[i];
duplicatesIndex++;
}
}
String[] checking = Arrays.copyOf(anIntArray, anIntArray.length);
for (String k : duplicates) {
checking = removeValue(checking, k);
}
return checking;
}
public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
//create copy of array, this will be your new array
//read array
//getNumberOfOccurences
//if getNumberOfOccurences appears more than once
//removeValue
//else
//store in newArray
//return values
/*private static final NULL_ARRAY = new String[0];
else if (anIntArray == null){
return NULL_ARRAY;
}*/
/*okay first we are making a new array, but a copy, it will copy this.array which is intArray and copy
its length (it takes two parameters)*/
int noDuplicates = 0;
String[] duplicates = new String[0];
int duplicatesIndex = 0;
for (int i = 0; i < anIntArray.length; i++) {
if (contains(duplicates, anIntArray[i])) {
continue;
} else if (getNumberOfOccurrences(anIntArray, anIntArray[i]) == exactNumberOfDuplications) {
duplicates = Arrays.copyOf(duplicates, duplicates.length + 1);
duplicates[duplicatesIndex] = array[i];
duplicatesIndex++;
}
}
String[] checking = Arrays.copyOf(anIntArray, anIntArray.length);
for (String k : duplicates) {
checking = removeValue(checking, k);
}
return checking;
}
public static int getNumberOfOccurrences(String[] array, String value) {
//created a countOccurence holder, to hold value everytime it appears
int countOccurence = 0;
for (String myValue : array) {
if(myValue.equals(value))
countOccurence++;
}
return countOccurence;
}
public static String[] removeValue(String[] array, String valueToRemove) {
/*1.we create this 'newSize' to hold the size of the array
2.now we have to create the array, with the new array size.
3.created a placeholder for things that were not duplicates
4.looping through the array
5. if the array indexes are not equal to valueToRemove
6. the next line basically swaps the array i for the other & we add to the counter
7. printing the new results */
int newSize = array.length - getNumberOfOccurrences(array, valueToRemove);
String[] outputArray = new String[newSize];
int indexNotDuplicates = 0;
for (int i =0; i <array.length; i++){
if(!array[i].equalsIgnoreCase(valueToRemove)){
outputArray[indexNotDuplicates] = array[i];
indexNotDuplicates++;
}
}
return outputArray;
}
public static boolean contains(String[] array, String value) {
/*the enhanced for loop starts at String myValue and searches to the end of array(so at every index comparing
. Then we add an if statement to see if the beginning of myValue equals value (which is what we are looking
for). */
for (String myValue : array) {
if(myValue.equals(value)){
return true;
}
}
return false;
}
}