|
|
@@ -1,6 +1,8 @@
|
|
1
|
1
|
package com.zipcodewilmington;
|
|
2
|
2
|
|
|
3
|
3
|
|
|
|
4
|
+import java.util.Arrays;
|
|
|
5
|
+
|
|
4
|
6
|
/**
|
|
5
|
7
|
* Created by leon on 1/29/18.
|
|
6
|
8
|
*/
|
|
|
@@ -124,13 +126,16 @@ public class StringArrayUtils {
|
|
124
|
126
|
|
|
125
|
127
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
126
|
128
|
String[] tempArray = new String[array.length];
|
|
|
129
|
+ int newIdx = 0;
|
|
127
|
130
|
for(int i = 0; i < array.length; i++){
|
|
128
|
|
- if(array[i] == valueToRemove) {
|
|
129
|
|
-
|
|
|
131
|
+ if(array[i] != valueToRemove) {
|
|
|
132
|
+ tempArray[newIdx] = array[i];
|
|
|
133
|
+ newIdx++;
|
|
130
|
134
|
}
|
|
131
|
135
|
|
|
132
|
136
|
}
|
|
133
|
|
- return array;
|
|
|
137
|
+ return Arrays.copyOf(tempArray, newIdx);
|
|
|
138
|
+
|
|
134
|
139
|
}
|
|
135
|
140
|
|
|
136
|
141
|
/**
|
|
|
@@ -138,8 +143,20 @@ public class StringArrayUtils {
|
|
138
|
143
|
* @return array of Strings with consecutive duplicates removes
|
|
139
|
144
|
*/ // TODO
|
|
140
|
145
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
141
|
|
- return null;
|
|
|
146
|
+ String[] tempArray = new String[array.length];
|
|
|
147
|
+ int newIdx = 0;
|
|
|
148
|
+
|
|
|
149
|
+ for(int i = 1; i < array.length; i++){
|
|
|
150
|
+ String current = array[i - 1];
|
|
|
151
|
+ if(!current.equals(array[i])){
|
|
|
152
|
+ tempArray[newIdx] = array[i - 1];
|
|
|
153
|
+ newIdx++;
|
|
|
154
|
+ }
|
|
142
|
155
|
}
|
|
|
156
|
+ tempArray[newIdx] = array[array.length - 1];
|
|
|
157
|
+ return Arrays.copyOf(tempArray, newIdx + 1);
|
|
|
158
|
+
|
|
|
159
|
+ }
|
|
143
|
160
|
|
|
144
|
161
|
|
|
145
|
162
|
|
|
|
@@ -148,10 +165,27 @@ public class StringArrayUtils {
|
|
148
|
165
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
149
|
166
|
*/ // TODO
|
|
150
|
167
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
151
|
|
- return null;
|
|
152
|
|
- }
|
|
|
168
|
+ String[] tempArray = new String[array.length];
|
|
|
169
|
+ int newIdx = 0;
|
|
|
170
|
+ String last = array[0];
|
|
|
171
|
+ String next = "";
|
|
|
172
|
+ String set = "";
|
|
|
173
|
+ for(int i = 0; i < array.length - 1; i++){
|
|
|
174
|
+ next = array[i+1];
|
|
|
175
|
+ if(next.equals(last)){
|
|
|
176
|
+ set += last;
|
|
|
177
|
+
|
|
|
178
|
+ } else {
|
|
|
179
|
+ tempArray[newIdx] = set;
|
|
|
180
|
+ set ="";
|
|
|
181
|
+ newIdx++;
|
|
|
182
|
+ }
|
|
153
|
183
|
|
|
|
184
|
+ }
|
|
154
|
185
|
|
|
|
186
|
+ return Arrays.copyOf(tempArray, newIdx + 1);
|
|
|
187
|
+
|
|
|
188
|
+ }
|
|
155
|
189
|
|
|
156
|
190
|
}
|
|
157
|
191
|
|