Explorar el Código

updated final method

David Thornley hace 6 años
padre
commit
4358aaa5ca
Se han modificado 1 ficheros con 27 adiciones y 6 borrados
  1. 27
    6
      StringArrayUtils.java

+ 27
- 6
StringArrayUtils.java Ver fichero

@@ -181,14 +181,35 @@ public class StringArrayUtils {
181 181
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
182 182
      */ // TODO
183 183
     public static String[] packConsecutiveDuplicates(String[] array) {
184
-        String saveMe = "";
185
-
186
-        for(int i = 0; i < array.length; i++){
187
-            saveMe = saveMe +array[i];
184
+        int counter = getCount(array);
185
+        String [] answer = new String[array.length - counter];
186
+        String previous = array[0];
187
+        answer[0] = array[0];
188
+        int index = 0;
189
+        
190
+        for(int i = 1; i < array.length; i++) {
191
+        if(array[i].equals(previous)){
192
+        answer[index] += array[i];
193
+        }else{
194
+        index++;
195
+        answer[index] = array[i];
196
+        previous = array[i];
188 197
         }
189
-        String[]answer = saveMe.split("(?<=(.))(?!\\1)");
190
-
198
+        }
199
+        
191 200
         return answer;
192 201
     }
193 202
 
203
+    public static int getCount (String[] array) {
204
+    
205
+        int count = 0;
206
+        for(int i = 0; i < array.length - 1; i++){
207
+            if(array[i].equals(array[i + 1])){
208
+                count++;
209
+            }
210
+        }
211
+
212
+        return count;
213
+    
214
+    }
194 215
 }