Ryan Small 8 лет назад
Родитель
Сommit
247ae961eb
1 измененных файлов: 53 добавлений и 3 удалений
  1. 53
    3
      StringArrayUtils.java

+ 53
- 3
StringArrayUtils.java Просмотреть файл

@@ -134,7 +134,7 @@ public class StringArrayUtils {
134 134
      * @return array of Strings with consecutive duplicates removes
135 135
      */ // TODO
136 136
     public static String[] removeConsecutiveDuplicates(String[] array) {
137
-        // ArrayList<String> remove = new ArrayList<String>();
137
+        
138 138
         // for(String e:array){
139 139
             // if(array[e] !=array[-1]){
140 140
                 // remove.removeAll();
@@ -143,7 +143,6 @@ public class StringArrayUtils {
143 143
         // return remove.toArray(new String[remove.add()]);
144 144
         
145 145
         
146
-        
147 146
         //Code below is skipping on index value
148 147
         // StringBuilder answerString = new StringBuilder();
149 148
         // for(int i = 1; i < array.length; i++){
@@ -162,6 +161,33 @@ public class StringArrayUtils {
162 161
         // }
163 162
         
164 163
         // return answerArray;
164
+        
165
+        
166
+        
167
+        
168
+        //loop through code
169
+        //compare all digits 
170
+        //if element appears more than once, count it
171
+        //if not, keep it pushing 
172
+        //ArrayList<String> remove = new ArrayList<String>();
173
+        
174
+                        
175
+        //String getAnswer = null;
176
+        //int counter = getCounter(array);
177
+        String [] answer = new String[array.length];
178
+        int index = 0;
179
+        answer[index]=array[0];
180
+        
181
+        index++; 
182
+        for(int i=1;i<array.length;i++){
183
+        if(!answer[index-1].equals(array[i])){
184
+            answer[index]=array[i];
185
+            index++; 
186
+        }
187
+        }
188
+        String [] blank = new String [index];  
189
+        System.arraycopy(answer, 0, blank, 0, index);
190
+        return blank; 
165 191
     }
166 192
 
167 193
     /**
@@ -169,7 +195,31 @@ public class StringArrayUtils {
169 195
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
170 196
      */ // TODO
171 197
     public static String[] packConsecutiveDuplicates(String[] array) {
172
-        return null;
198
+        StringBuilder pack = new StringBuilder(array[0]);
199
+        String [] answer = new String[array.length-getCounter(array)];
200
+        int index = 0;  
201
+        for(int i=1;i<array.length;i++){
202
+        if(array[i-1].equals(array[i])){
203
+            pack.append(array[i]);
204
+        }else{
205
+            answer[index] = pack.toString();
206
+            pack.setLength(0);
207
+            pack.append(array[i]);
208
+            index++; 
209
+        }
210
+        }
211
+        
212
+        answer[index]=pack.toString();
213
+        return answer ;
173 214
     }
174 215
 
216
+    public static int getCounter(String [] array){
217
+    int counter = 0; 
218
+    for(int i=1;i<array.length;i++){
219
+        if(array[i].equals(array[i-1])){
220
+        counter+=1;
221
+        }
222
+        }
223
+        return counter; 
224
+    }
175 225
 }