|
@@ -1,5 +1,10 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+import java.util.LinkedList;
|
|
6
|
+import java.util.List;
|
|
7
|
+
|
3
|
8
|
/**
|
4
|
9
|
* Created by leon on 1/29/18.
|
5
|
10
|
*/
|
|
@@ -9,7 +14,8 @@ public class StringArrayUtils {
|
9
|
14
|
* @return first element of specified array
|
10
|
15
|
*/ // TODO
|
11
|
16
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
17
|
+
|
|
18
|
+ return array[0];
|
13
|
19
|
}
|
14
|
20
|
|
15
|
21
|
/**
|
|
@@ -17,7 +23,8 @@ public class StringArrayUtils {
|
17
|
23
|
* @return second element in specified array
|
18
|
24
|
*/
|
19
|
25
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
26
|
+
|
|
27
|
+ return array[1];
|
21
|
28
|
}
|
22
|
29
|
|
23
|
30
|
/**
|
|
@@ -25,7 +32,8 @@ 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
|
+
|
|
36
|
+ return array[array.length - 1];
|
29
|
37
|
}
|
30
|
38
|
|
31
|
39
|
/**
|
|
@@ -33,7 +41,8 @@ public class StringArrayUtils {
|
33
|
41
|
* @return second to last element in specified array
|
34
|
42
|
*/ // TODO
|
35
|
43
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
44
|
+
|
|
45
|
+ return array[array.length - 2];
|
37
|
46
|
}
|
38
|
47
|
|
39
|
48
|
/**
|
|
@@ -42,7 +51,9 @@ public class StringArrayUtils {
|
42
|
51
|
* @return true if the array contains the specified `value`
|
43
|
52
|
*/ // TODO
|
44
|
53
|
public static boolean contains(String[] array, String value) {
|
45
|
|
- return false;
|
|
54
|
+
|
|
55
|
+ boolean contains = Arrays.stream(array).anyMatch(value::equals);
|
|
56
|
+ return contains;
|
46
|
57
|
}
|
47
|
58
|
|
48
|
59
|
/**
|
|
@@ -50,7 +61,15 @@ public class StringArrayUtils {
|
50
|
61
|
* @return an array with identical contents in reverse order
|
51
|
62
|
*/ // TODO
|
52
|
63
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
64
|
+ String[] newArray = new String[array.length];
|
|
65
|
+ int leng = 0;
|
|
66
|
+
|
|
67
|
+ for (int i = array.length - 1; i >= 0; i--) {
|
|
68
|
+ newArray[leng] = array[i];
|
|
69
|
+ leng++;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ return newArray;
|
54
|
73
|
}
|
55
|
74
|
|
56
|
75
|
/**
|
|
@@ -58,7 +77,9 @@ public class StringArrayUtils {
|
58
|
77
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
78
|
*/ // TODO
|
60
|
79
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+ return Arrays.equals(array, StringArrayUtils.reverse(array));
|
62
|
83
|
}
|
63
|
84
|
|
64
|
85
|
/**
|
|
@@ -66,16 +87,38 @@ public class StringArrayUtils {
|
66
|
87
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
88
|
*/ // TODO
|
68
|
89
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
90
|
+
|
|
91
|
+ String alphaString = Arrays.toString(array).toLowerCase();
|
|
92
|
+ boolean result= false;
|
|
93
|
+ for (char l = 'a'; l <= 'z'; l++) {
|
|
94
|
+ if (!alphaString.contains(String.valueOf(l))) {
|
|
95
|
+ result= false;
|
|
96
|
+ }
|
|
97
|
+ else {
|
|
98
|
+ result= true;
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+ return result;
|
70
|
105
|
}
|
71
|
106
|
|
72
|
|
- /**
|
|
107
|
+ /**i=
|
73
|
108
|
* @param array array of String objects
|
74
|
109
|
* @param value value to check array for
|
75
|
110
|
* @return number of occurrences the specified `value` has occurred
|
76
|
111
|
*/ // TODO
|
77
|
112
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
113
|
+
|
|
114
|
+ int count = 0;
|
|
115
|
+ for (int i = 0; i < array.length; i++) {
|
|
116
|
+ if (array[i] == value) {
|
|
117
|
+ count++;
|
|
118
|
+
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+ return count;
|
79
|
122
|
}
|
80
|
123
|
|
81
|
124
|
/**
|
|
@@ -84,7 +127,18 @@ public class StringArrayUtils {
|
84
|
127
|
* @return array with identical contents excluding values of `value`
|
85
|
128
|
*/ // TODO
|
86
|
129
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+ String[] newArray = new String[array.length - 1];
|
|
133
|
+ int indexN= 0;
|
|
134
|
+ for (int i = 0; i < array.length; i++) {
|
|
135
|
+ if (!valueToRemove.equals(array[i])) {
|
|
136
|
+ newArray[indexN] = array[i];
|
|
137
|
+ indexN= indexN + 1;
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ }
|
|
141
|
+ return newArray;
|
88
|
142
|
}
|
89
|
143
|
|
90
|
144
|
/**
|
|
@@ -92,16 +146,40 @@ public class StringArrayUtils {
|
92
|
146
|
* @return array of Strings with consecutive duplicates removes
|
93
|
147
|
*/ // TODO
|
94
|
148
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
96
|
|
- }
|
|
149
|
+ StringBuilder builder = new StringBuilder();
|
|
150
|
+ String duplicate = "";
|
|
151
|
+ for (String s : array) {
|
|
152
|
+ if (!s.equals(duplicate)) {
|
|
153
|
+ builder.append(s + " ");
|
|
154
|
+ }
|
|
155
|
+ duplicate = s;
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+ String newArray[] = builder.toString().split(" ");
|
|
160
|
+ return newArray;
|
|
161
|
+}
|
97
|
162
|
|
98
|
163
|
/**
|
99
|
164
|
* @param array array of chars
|
100
|
165
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
166
|
*/ // TODO
|
102
|
167
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
104
|
|
- }
|
|
168
|
+ ArrayList<String> arrayList = new ArrayList<>();
|
|
169
|
+
|
|
170
|
+ arrayList.add(array[0]);
|
|
171
|
+ for(int i = 1; i < array.length; i++) {
|
|
172
|
+ if(array[i].equals(array[i-1])) {
|
|
173
|
+ String str = arrayList.get(arrayList.size() - 1).concat(array[i]);
|
|
174
|
+ arrayList.set(arrayList.size() - 1, str);
|
|
175
|
+ } else
|
|
176
|
+ {
|
|
177
|
+ arrayList.add(array[i]);
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+ String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
|
|
181
|
+ return strReturn;
|
|
182
|
+ }
|
105
|
183
|
|
106
|
184
|
|
107
|
185
|
}
|