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