|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
import java.util.Arrays;
|
|
2
|
2
|
import java.io.*;
|
|
3
|
|
-import java.util.*;
|
|
|
3
|
+import java.util.*;
|
|
|
4
|
+import java.lang.StringBuilder;
|
|
4
|
5
|
|
|
5
|
6
|
/**
|
|
6
|
7
|
* Created by leon on 1/29/18.
|
|
|
@@ -84,7 +85,7 @@ public class StringArrayUtils {
|
|
84
|
85
|
*/ // TODO
|
|
85
|
86
|
public static boolean isPangramic(String[] array) {
|
|
86
|
87
|
String arrayString = Arrays.toString(array).toLowerCase();
|
|
87
|
|
-
|
|
|
88
|
+
|
|
88
|
89
|
if(arrayString.length() < 26){
|
|
89
|
90
|
return false;
|
|
90
|
91
|
}
|
|
|
@@ -94,7 +95,7 @@ public class StringArrayUtils {
|
|
94
|
95
|
return false;
|
|
95
|
96
|
}
|
|
96
|
97
|
}
|
|
97
|
|
-
|
|
|
98
|
+
|
|
98
|
99
|
return true;
|
|
99
|
100
|
}
|
|
100
|
101
|
|
|
|
@@ -111,7 +112,7 @@ public class StringArrayUtils {
|
|
111
|
112
|
numberOfOccurrences += 1;
|
|
112
|
113
|
}
|
|
113
|
114
|
}
|
|
114
|
|
-
|
|
|
115
|
+
|
|
115
|
116
|
return numberOfOccurrences;
|
|
116
|
117
|
}
|
|
117
|
118
|
|
|
|
@@ -123,7 +124,7 @@ public class StringArrayUtils {
|
|
123
|
124
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
124
|
125
|
int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
|
|
125
|
126
|
String[] newArray = new String[array.length - numberOfOccurrences];
|
|
126
|
|
-
|
|
|
127
|
+
|
|
127
|
128
|
int i = 0;
|
|
128
|
129
|
for(String element : array){
|
|
129
|
130
|
if(!element.equals(valueToRemove)){
|
|
|
@@ -133,7 +134,7 @@ public class StringArrayUtils {
|
|
133
|
134
|
}
|
|
134
|
135
|
return newArray;
|
|
135
|
136
|
}
|
|
136
|
|
-
|
|
|
137
|
+
|
|
137
|
138
|
public static int numberOfConsecutiveDuplicates(String[] array) {
|
|
138
|
139
|
int numberOfConsecDups = 0;
|
|
139
|
140
|
for(int i=1; i<=array.length-1; i++){
|
|
|
@@ -149,7 +150,17 @@ public class StringArrayUtils {
|
|
149
|
150
|
* @return array of Strings with consecutive duplicates removes
|
|
150
|
151
|
*/ // TODO
|
|
151
|
152
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
152
|
|
-
|
|
|
153
|
+ ArrayList<String> newList = new ArrayList<String>();
|
|
|
154
|
+ //add first element of array to new array
|
|
|
155
|
+ newList.add(array[0]);
|
|
|
156
|
+ //compare each element in your array starting with the second
|
|
|
157
|
+ for (int i=1; i<=array.length-1; i++){
|
|
|
158
|
+ //if an element does not equal an element before it, add to new array
|
|
|
159
|
+ if(!(array[i].equals(array[i-1]))){
|
|
|
160
|
+ newList.add(array[i]);
|
|
|
161
|
+ }
|
|
|
162
|
+ }
|
|
|
163
|
+ return newList.toArray(new String[0]);
|
|
153
|
164
|
}
|
|
154
|
165
|
|
|
155
|
166
|
/**
|
|
|
@@ -157,7 +168,25 @@ public class StringArrayUtils {
|
|
157
|
168
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
158
|
169
|
*/ // TODO
|
|
159
|
170
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
160
|
|
- return null;
|
|
|
171
|
+ //create a new string using string builder
|
|
|
172
|
+ StringBuilder arrayAsString = new StringBuilder();
|
|
|
173
|
+ //add first element in array to new array string
|
|
|
174
|
+ arrayAsString.append(array[0]);
|
|
|
175
|
+ //look at each element in the array
|
|
|
176
|
+ for(int i=1; i<=array.length-1; i++){
|
|
|
177
|
+ //if the element in the array equals the previous element, add it to
|
|
|
178
|
+ // the string
|
|
|
179
|
+ if(array[i].equals(array[i-1])){
|
|
|
180
|
+ arrayAsString.append(array[i]);
|
|
|
181
|
+ } else {
|
|
|
182
|
+ //if the element in the array does not equal the previous element,
|
|
|
183
|
+ // add it to the string after a space
|
|
|
184
|
+ arrayAsString.append(" ");
|
|
|
185
|
+ arrayAsString.append(array[i]);
|
|
|
186
|
+ }
|
|
|
187
|
+ }
|
|
|
188
|
+ //convert the new string to a space split string array
|
|
|
189
|
+ String[] packConsecDupArray = arrayAsString.toString().split(" ");
|
|
|
190
|
+ return packConsecDupArray;
|
|
161
|
191
|
}
|
|
162
|
|
-
|
|
163
|
192
|
}
|