|
|
@@ -3,13 +3,18 @@
|
|
3
|
3
|
/**
|
|
4
|
4
|
* Created by leon on 1/29/18.
|
|
5
|
5
|
*/
|
|
|
6
|
+
|
|
|
7
|
+import java.util.Collections;
|
|
|
8
|
+
|
|
6
|
9
|
public class StringArrayUtils {
|
|
7
|
10
|
/**
|
|
8
|
11
|
* @param array array of String objects
|
|
9
|
12
|
* @return first element of specified array
|
|
10
|
13
|
*/ // TODO
|
|
11
|
14
|
public static String getFirstElement(String[] array) {
|
|
12
|
|
- return null;
|
|
|
15
|
+ String firstElement = array[0];
|
|
|
16
|
+
|
|
|
17
|
+ return firstElement;
|
|
13
|
18
|
}
|
|
14
|
19
|
|
|
15
|
20
|
/**
|
|
|
@@ -17,7 +22,9 @@ public class StringArrayUtils {
|
|
17
|
22
|
* @return second element in specified array
|
|
18
|
23
|
*/
|
|
19
|
24
|
public static String getSecondElement(String[] array) {
|
|
20
|
|
- return null;
|
|
|
25
|
+ String secondElement = array[1];
|
|
|
26
|
+
|
|
|
27
|
+ return secondElement;
|
|
21
|
28
|
}
|
|
22
|
29
|
|
|
23
|
30
|
/**
|
|
|
@@ -25,7 +32,9 @@ public class StringArrayUtils {
|
|
25
|
32
|
* @return last element in specified array
|
|
26
|
33
|
*/ // TODO
|
|
27
|
34
|
public static String getLastElement(String[] array) {
|
|
28
|
|
- return null;
|
|
|
35
|
+ String lastElement = array[array.length-1];
|
|
|
36
|
+
|
|
|
37
|
+ return lastElement;
|
|
29
|
38
|
}
|
|
30
|
39
|
|
|
31
|
40
|
/**
|
|
|
@@ -33,7 +42,8 @@ public class StringArrayUtils {
|
|
33
|
42
|
* @return second to last element in specified array
|
|
34
|
43
|
*/ // TODO
|
|
35
|
44
|
public static String getSecondToLastElement(String[] array) {
|
|
36
|
|
- return null;
|
|
|
45
|
+ String secondToLast = array[array.length-2];
|
|
|
46
|
+ return secondToLast;
|
|
37
|
47
|
}
|
|
38
|
48
|
|
|
39
|
49
|
/**
|
|
|
@@ -42,7 +52,14 @@ public class StringArrayUtils {
|
|
42
|
52
|
* @return true if the array contains the specified `value`
|
|
43
|
53
|
*/ // TODO
|
|
44
|
54
|
public static boolean contains(String[] array, String value) {
|
|
45
|
|
- return false;
|
|
|
55
|
+ boolean contains = false;
|
|
|
56
|
+
|
|
|
57
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
58
|
+ if (array[i].equalsIgnoreCase(value)) {
|
|
|
59
|
+ contains = true;
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+ return contains;
|
|
46
|
63
|
}
|
|
47
|
64
|
|
|
48
|
65
|
/**
|
|
|
@@ -50,7 +67,15 @@ public class StringArrayUtils {
|
|
50
|
67
|
* @return an array with identical contents in reverse order
|
|
51
|
68
|
*/ // TODO
|
|
52
|
69
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
70
|
+
|
|
|
71
|
+ String temp;
|
|
|
72
|
+ for (int i = 0; i <= array.length/2; i++) {
|
|
|
73
|
+ temp = array[i];
|
|
|
74
|
+ array[i] = array[array.length -1 -i];
|
|
|
75
|
+ array[array.length -1 -i] = temp;
|
|
|
76
|
+
|
|
|
77
|
+ }
|
|
|
78
|
+ return array;
|
|
54
|
79
|
}
|
|
55
|
80
|
|
|
56
|
81
|
/**
|
|
|
@@ -58,7 +83,15 @@ public class StringArrayUtils {
|
|
58
|
83
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
84
|
*/ // TODO
|
|
60
|
85
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
86
|
+ boolean palindrome = false;
|
|
|
87
|
+ for (int i = 0; i <= array.length/2; i++) {
|
|
|
88
|
+ if (array[i] == array[array.length -1 -i]) {
|
|
|
89
|
+ palindrome = true;
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+
|
|
|
93
|
+ }
|
|
|
94
|
+ return palindrome;
|
|
62
|
95
|
}
|
|
63
|
96
|
|
|
64
|
97
|
/**
|
|
|
@@ -66,7 +99,25 @@ public class StringArrayUtils {
|
|
66
|
99
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
100
|
*/ // TODO
|
|
68
|
101
|
public static boolean isPangramic(String[] array) {
|
|
69
|
|
- return false;
|
|
|
102
|
+ boolean pangram = false;
|
|
|
103
|
+
|
|
|
104
|
+ StringBuilder build = new StringBuilder();
|
|
|
105
|
+
|
|
|
106
|
+ for (String s : array){
|
|
|
107
|
+ build.append(s);
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ String ofArr = "";
|
|
|
111
|
+ ofArr = build.toString();
|
|
|
112
|
+ for (char a = 'A'; a <= 'Z'; a++) {
|
|
|
113
|
+ if ((ofArr.indexOf(a) < 0) && (ofArr.indexOf((char)(a + 32)) < 0)) {
|
|
|
114
|
+ pangram = false;
|
|
|
115
|
+ } else {
|
|
|
116
|
+ pangram = true;
|
|
|
117
|
+
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+ return pangram;
|
|
70
|
121
|
}
|
|
71
|
122
|
|
|
72
|
123
|
/**
|
|
|
@@ -75,7 +126,16 @@ public class StringArrayUtils {
|
|
75
|
126
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
127
|
*/ // TODO
|
|
77
|
128
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
129
|
+ int count = 0;
|
|
|
130
|
+
|
|
|
131
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
132
|
+ if (array[i] == value) {
|
|
|
133
|
+ count++;
|
|
|
134
|
+ }
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+
|
|
|
138
|
+ return count;
|
|
79
|
139
|
}
|
|
80
|
140
|
|
|
81
|
141
|
/**
|
|
|
@@ -84,7 +144,20 @@ public class StringArrayUtils {
|
|
84
|
144
|
* @return array with identical contents excluding values of `value`
|
|
85
|
145
|
*/ // TODO
|
|
86
|
146
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
147
|
+ int index = 0;
|
|
|
148
|
+ int count = getNumberOfOccurrences(array, valueToRemove);
|
|
|
149
|
+ String [] newArr = new String [array.length - count] ;
|
|
|
150
|
+
|
|
|
151
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
152
|
+ if (array[i] != valueToRemove) {
|
|
|
153
|
+
|
|
|
154
|
+ newArr[index] = array[i];
|
|
|
155
|
+ index++;
|
|
|
156
|
+ }
|
|
|
157
|
+ }
|
|
|
158
|
+ return newArr;
|
|
|
159
|
+
|
|
|
160
|
+
|
|
88
|
161
|
}
|
|
89
|
162
|
|
|
90
|
163
|
/**
|
|
|
@@ -92,7 +165,30 @@ public class StringArrayUtils {
|
|
92
|
165
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
166
|
*/ // TODO
|
|
94
|
167
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
168
|
+ String last = null;
|
|
|
169
|
+ int index = 0;
|
|
|
170
|
+ int count = 0;
|
|
|
171
|
+
|
|
|
172
|
+
|
|
|
173
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
174
|
+ if (array[i].equals(array[i + 1])) {
|
|
|
175
|
+ count++;
|
|
|
176
|
+ }
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ String [] newArr = new String [array.length - count];
|
|
|
180
|
+
|
|
|
181
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
182
|
+
|
|
|
183
|
+
|
|
|
184
|
+ if (!array[i].equals(last)){
|
|
|
185
|
+ newArr[index] = array[i];
|
|
|
186
|
+ last = array[i];
|
|
|
187
|
+ index++;
|
|
|
188
|
+ }
|
|
|
189
|
+ }
|
|
|
190
|
+
|
|
|
191
|
+ return newArr;
|
|
96
|
192
|
}
|
|
97
|
193
|
|
|
98
|
194
|
/**
|
|
|
@@ -100,8 +196,37 @@ public class StringArrayUtils {
|
|
100
|
196
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
101
|
197
|
*/ // TODO
|
|
102
|
198
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
103
|
|
- return null;
|
|
|
199
|
+ int getCounting = getCount(array);
|
|
|
200
|
+ String [] savedArr = new String [array.length - getCounting];
|
|
|
201
|
+ String last = array[0];
|
|
|
202
|
+ savedArr[0] = array[0];
|
|
|
203
|
+ int index = 0;
|
|
|
204
|
+
|
|
|
205
|
+ for (int i = 1; i < array.length; i++) {
|
|
|
206
|
+ if (array[i].equals(last)) {
|
|
|
207
|
+ savedArr[index] += array[i];
|
|
|
208
|
+ }
|
|
|
209
|
+ else {
|
|
|
210
|
+ index++;
|
|
|
211
|
+ savedArr[index] = array[i];
|
|
|
212
|
+ last = array[i];
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
|
215
|
+ }
|
|
|
216
|
+
|
|
|
217
|
+ return savedArr;
|
|
|
218
|
+
|
|
104
|
219
|
}
|
|
105
|
220
|
|
|
106
|
|
-
|
|
|
221
|
+ private static int getCount (String[] array){
|
|
|
222
|
+ int count = 0;
|
|
|
223
|
+
|
|
|
224
|
+
|
|
|
225
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
|
226
|
+ if (array[i].equals(array[i + 1])) {
|
|
|
227
|
+ count++;
|
|
|
228
|
+ }
|
|
|
229
|
+ }
|
|
|
230
|
+ return count;
|
|
|
231
|
+ }
|
|
107
|
232
|
}
|