Browse Source

Working on methods

Trinh Tong 8 years ago
parent
commit
ef499962de
1 changed files with 20 additions and 3 deletions
  1. 20
    3
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 20
- 3
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

@@ -183,9 +183,26 @@ public class StringArrayUtils {
183 183
      */ // TODO
184 184
     public static String[] packConsecutiveDuplicates(String[] array) {
185 185
 
186
-        // First a loop to check for duplicates
187
-        
188
-        return null;
186
+        String[] newArray = new String[array.length];
187
+        String firstCompare = array[0];
188
+        String secondCompare = array[0];
189
+
190
+        int newLength = 0;
191
+
192
+        for (int i = 1; i < array.length; i++) {
193
+            if (array[i].equals(firstCompare) == false) {
194
+                firstCompare = array[i];
195
+                newArray[newLength] = secondCompare;
196
+                secondCompare = array[i];
197
+                newLength++;
198
+            } else {
199
+                secondCompare += array[i];
200
+            }
201
+        }
202
+
203
+        newArray[newLength] = secondCompare;
204
+
205
+        return Arrays.copyOf(newArray,  newLength + 1);
189 206
     }
190 207
 
191 208