rayskeez21 8 vuotta sitten
vanhempi
commit
92060590a8
1 muutettua tiedostoa jossa 37 lisäystä ja 12 poistoa
  1. 37
    12
      StringArrayUtils.java

+ 37
- 12
StringArrayUtils.java Näytä tiedosto

@@ -145,32 +145,57 @@ public class StringArrayUtils {
145 145
     public static String[] removeConsecutiveDuplicates(String[] array) {
146 146
         int count = 0;
147 147
         int duplicate = 0;
148
-        
149
-        for(int i = 0; i < array.length; i++){
148
+
149
+        for(int i = 0; i < array.length - 1; i++){
150 150
             if (array[i].equals(array[i+1])){
151 151
                 count++;
152 152
             }
153 153
         }
154
-        
155
-        String []arr = new String [array.length - counter];
156
-        String strCheese = 
154
+
155
+        String []arr = new String [array.length - count];
156
+        String strCheese = null;
157 157
         for(int i= 0;i < array.length; i++){
158
-            if(!array[i].equals()){
159
-            
158
+            if(!array[i].equals(strCheese)){
159
+                arr[duplicate] = array[i];
160
+                strCheese = array[i];
161
+                duplicate++;
160 162
             }
161
-        
163
+
162 164
         }
163
-        return null;
164
-        
165
+        return arr;
165 166
 
166 167
     }
167
-}
168 168
     /**
169 169
      * @param array array of chars
170 170
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
171 171
      */ // TODO
172 172
     public static String[] packConsecutiveDuplicates(String[] array) {
173
-        return null;
173
+        int count = getCount(array);
174
+        String [] result = new String[array.length - count];
175
+        String last = array[0];
176
+        result[0] = array[0];
177
+        int index = 0;
178
+        
179
+        for (int i = 1; i < array.length; i++){
180
+        if (array[i].equals(last)){
181
+            result[index] += array[i];
182
+        }else{
183
+            index++;
184
+            result[index] = array[i];
185
+            last = array[i];
186
+        }
187
+        }
188
+        return result;
189
+    }
190
+
191
+    public static int getCount(String [] array){
192
+        int count = 0;
193
+        for(int i = 0; i < array.length - 1; i++){
194
+            if(array[i].equals(array[i+1])){
195
+                count++;
196
+            }
197
+        }
198
+        return count;
174 199
     }
175 200
 
176 201
 }