|
|
@@ -1,5 +1,6 @@
|
|
1
|
|
-
|
|
2
|
|
-
|
|
|
1
|
+import java.util.ArrayList;
|
|
|
2
|
+import java.util.Arrays;
|
|
|
3
|
+import java.util.List;
|
|
3
|
4
|
/**
|
|
4
|
5
|
* Created by leon on 1/29/18.
|
|
5
|
6
|
*/
|
|
|
@@ -9,7 +10,7 @@ public class StringArrayUtils {
|
|
9
|
10
|
* @return first element of specified array
|
|
10
|
11
|
*/ // TODO
|
|
11
|
12
|
public static String getFirstElement(String[] array) {
|
|
12
|
|
- return null;
|
|
|
13
|
+ return array[0];
|
|
13
|
14
|
}
|
|
14
|
15
|
|
|
15
|
16
|
/**
|
|
|
@@ -17,7 +18,7 @@ public class StringArrayUtils {
|
|
17
|
18
|
* @return second element in specified array
|
|
18
|
19
|
*/
|
|
19
|
20
|
public static String getSecondElement(String[] array) {
|
|
20
|
|
- return null;
|
|
|
21
|
+ return array[1];
|
|
21
|
22
|
}
|
|
22
|
23
|
|
|
23
|
24
|
/**
|
|
|
@@ -25,7 +26,7 @@ public class StringArrayUtils {
|
|
25
|
26
|
* @return last element in specified array
|
|
26
|
27
|
*/ // TODO
|
|
27
|
28
|
public static String getLastElement(String[] array) {
|
|
28
|
|
- return null;
|
|
|
29
|
+ return array[array.length - 1];
|
|
29
|
30
|
}
|
|
30
|
31
|
|
|
31
|
32
|
/**
|
|
|
@@ -33,7 +34,7 @@ public class StringArrayUtils {
|
|
33
|
34
|
* @return second to last element in specified array
|
|
34
|
35
|
*/ // TODO
|
|
35
|
36
|
public static String getSecondToLastElement(String[] array) {
|
|
36
|
|
- return null;
|
|
|
37
|
+ return array[array.length - 2];
|
|
37
|
38
|
}
|
|
38
|
39
|
|
|
39
|
40
|
/**
|
|
|
@@ -42,6 +43,11 @@ public class StringArrayUtils {
|
|
42
|
43
|
* @return true if the array contains the specified `value`
|
|
43
|
44
|
*/ // TODO
|
|
44
|
45
|
public static boolean contains(String[] array, String value) {
|
|
|
46
|
+ for (String ithString : array) {
|
|
|
47
|
+ if (ithString.equals(value)) {
|
|
|
48
|
+ return true;
|
|
|
49
|
+ }
|
|
|
50
|
+ }
|
|
45
|
51
|
return false;
|
|
46
|
52
|
}
|
|
47
|
53
|
|
|
|
@@ -50,7 +56,11 @@ public class StringArrayUtils {
|
|
50
|
56
|
* @return an array with identical contents in reverse order
|
|
51
|
57
|
*/ // TODO
|
|
52
|
58
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
59
|
+ String[] reverse = new String[array.length];
|
|
|
60
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
61
|
+ reverse[i] = array[array.length - 1 - i];
|
|
|
62
|
+ }
|
|
|
63
|
+ return reverse;
|
|
54
|
64
|
}
|
|
55
|
65
|
|
|
56
|
66
|
/**
|
|
|
@@ -58,7 +68,13 @@ public class StringArrayUtils {
|
|
58
|
68
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
69
|
*/ // TODO
|
|
60
|
70
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
71
|
+ String[] reverse = reverse(array);
|
|
|
72
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
73
|
+ if (!array[i].equals(reverse[i])) {
|
|
|
74
|
+ return false;
|
|
|
75
|
+ }
|
|
|
76
|
+ }
|
|
|
77
|
+ return true;
|
|
62
|
78
|
}
|
|
63
|
79
|
|
|
64
|
80
|
/**
|
|
|
@@ -66,6 +82,20 @@ public class StringArrayUtils {
|
|
66
|
82
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
83
|
*/ // TODO
|
|
68
|
84
|
public static boolean isPangramic(String[] array) {
|
|
|
85
|
+ //stringbuilder holding all letters of alpha
|
|
|
86
|
+ StringBuilder alphabet = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
|
|
|
87
|
+ //iterate over each word of the array and then each char of word
|
|
|
88
|
+ for (String word : array) {
|
|
|
89
|
+ for (int i = 0; i < word.length(); i++) {
|
|
|
90
|
+ int index = alphabet.indexOf("" + word.toLowerCase().charAt(i));
|
|
|
91
|
+ if (index >= 0) {
|
|
|
92
|
+ alphabet.deleteCharAt(index);
|
|
|
93
|
+ }
|
|
|
94
|
+ if (alphabet.length() == 0) {
|
|
|
95
|
+ return true;
|
|
|
96
|
+ }
|
|
|
97
|
+ }
|
|
|
98
|
+ }
|
|
69
|
99
|
return false;
|
|
70
|
100
|
}
|
|
71
|
101
|
|
|
|
@@ -75,7 +105,13 @@ public class StringArrayUtils {
|
|
75
|
105
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
106
|
*/ // TODO
|
|
77
|
107
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
108
|
+ int occurrences = 0;
|
|
|
109
|
+ for (String word : array) {
|
|
|
110
|
+ if (value.equals(word)) {
|
|
|
111
|
+ occurrences++;
|
|
|
112
|
+ }
|
|
|
113
|
+ }
|
|
|
114
|
+ return occurrences;
|
|
79
|
115
|
}
|
|
80
|
116
|
|
|
81
|
117
|
/**
|
|
|
@@ -84,7 +120,16 @@ public class StringArrayUtils {
|
|
84
|
120
|
* @return array with identical contents excluding values of `value`
|
|
85
|
121
|
*/ // TODO
|
|
86
|
122
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
123
|
+ List<String> arrRemoved = new ArrayList<String>();
|
|
|
124
|
+ for (String word : array) {
|
|
|
125
|
+ if (!word.equals(valueToRemove)) {
|
|
|
126
|
+ arrRemoved.add(word);
|
|
|
127
|
+ }
|
|
|
128
|
+ }
|
|
|
129
|
+ String[] removedArray = new String[arrRemoved.size()];
|
|
|
130
|
+ removedArray = arrRemoved.toArray(removedArray);
|
|
|
131
|
+ return removedArray;
|
|
|
132
|
+
|
|
88
|
133
|
}
|
|
89
|
134
|
|
|
90
|
135
|
/**
|
|
|
@@ -92,7 +137,17 @@ public class StringArrayUtils {
|
|
92
|
137
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
138
|
*/ // TODO
|
|
94
|
139
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
140
|
+ List<String> removedValues = new ArrayList<String>();
|
|
|
141
|
+ for (String word : array) {
|
|
|
142
|
+ //empty or last added value doesnt equal iterated word of array
|
|
|
143
|
+ if (removedValues.isEmpty() ||
|
|
|
144
|
+ !removedValues.get(removedValues.size() - 1).equals(word)) {
|
|
|
145
|
+ removedValues.add(word);
|
|
|
146
|
+ }
|
|
|
147
|
+ }
|
|
|
148
|
+ String[] nonDuplicate = new String[removedValues.size()];
|
|
|
149
|
+ nonDuplicate = removedValues.toArray(nonDuplicate);
|
|
|
150
|
+ return nonDuplicate;
|
|
96
|
151
|
}
|
|
97
|
152
|
|
|
98
|
153
|
/**
|
|
|
@@ -100,8 +155,10 @@ public class StringArrayUtils {
|
|
100
|
155
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
101
|
156
|
*/ // TODO
|
|
102
|
157
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
|
158
|
+ for (String singleChar : array) {
|
|
|
159
|
+
|
|
|
160
|
+ }
|
|
103
|
161
|
return null;
|
|
104
|
162
|
}
|
|
105
|
163
|
|
|
106
|
|
-
|
|
107
|
164
|
}
|