StringArrayUtils.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import java.util.Arrays;
  2. /**
  3. * Created by leon on 1/29/18.
  4. */
  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. return array[0];
  12. }
  13. /**
  14. * @param array array of String objects
  15. * @return second element in specified array
  16. */
  17. public static String getSecondElement(String[] array) {
  18. return array[1];
  19. }
  20. /**
  21. * @param array array of String objects
  22. * @return last element in specified array
  23. */ // TODO
  24. public static String getLastElement(String[] array) {
  25. return array[array.length-1];
  26. }
  27. /**
  28. * @param array array of String objects
  29. * @return second to last element in specified array
  30. */ // TODO
  31. public static String getSecondToLastElement(String[] array) {
  32. return array[array.length-2];
  33. }
  34. /**
  35. * @param array array of String objects
  36. * @param value value to check array for
  37. * @return true if the array contains the specified `value`
  38. */ // TODO
  39. public static boolean contains(String[] array, String value) {
  40. boolean found = false;
  41. for (String currVal : array) {
  42. if (currVal.equals(value)) {
  43. found = true;
  44. break;
  45. }
  46. }
  47. return found;
  48. }
  49. /**
  50. * @param array of String objects
  51. * @return an array with identical contents in reverse order
  52. */ // TODO
  53. public static String[] reverse(String[] array) {
  54. String[] reverse = new String[array.length];
  55. for (int i = 0; i < array.length; i++) {
  56. reverse[i] = array[array.length-1-i];
  57. }
  58. return reverse;
  59. }
  60. /**
  61. * @param array array of String objects
  62. * @return true if the order of the array is the same backwards and forwards
  63. */ // TODO
  64. public static boolean isPalindromic(String[] array) {
  65. return Arrays.deepEquals(array, reverse(array));
  66. }
  67. /**
  68. * @param array array of String objects
  69. * @return true if each letter in the alphabet has been used in the array
  70. */ // TODO
  71. public static boolean isPangramic(String[] array) {
  72. String[] abc = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  73. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  74. "w", "x", "y", "z" };
  75. boolean pangramic = true;
  76. for (String letter : abc) {
  77. boolean thisLetterFound = false;
  78. for (String string : array) {
  79. if (string.toLowerCase().contains(letter)) {
  80. thisLetterFound = true;
  81. break;
  82. }
  83. }
  84. if (!thisLetterFound) {
  85. pangramic = false;
  86. break;
  87. }
  88. }
  89. return pangramic;
  90. }
  91. /**
  92. * @param array array of String objects
  93. * @param value value to check array for
  94. * @return number of occurrences the specified `value` has occurred
  95. */ // TODO
  96. public static int getNumberOfOccurrences(String[] array, String value) {
  97. int count = 0;
  98. for (String string : array){
  99. if (string.equals(value)) count++;
  100. }
  101. return count;
  102. }
  103. /**
  104. * @param array array of String objects
  105. * @param valueToRemove value to remove from array
  106. * @return array with identical contents excluding values of `value`
  107. */ // TODO
  108. public static String[] removeValue(String[] array, String valueToRemove) {
  109. int numOccurrences = getNumberOfOccurrences(array, valueToRemove);
  110. String[] arrRemoved;
  111. if (numOccurrences == 0) {
  112. arrRemoved = array;
  113. }
  114. else {
  115. arrRemoved = new String[array.length - numOccurrences];
  116. int removedArrPos = 0;
  117. for (int i = 0; i < array.length; i++) {
  118. if (!array[i].equals(valueToRemove)) {
  119. arrRemoved[removedArrPos] = array[i];
  120. removedArrPos++;
  121. }
  122. }
  123. }
  124. return arrRemoved;
  125. }
  126. /**
  127. * @param array array of chars
  128. * @return array of Strings with consecutive duplicates removes
  129. */ // TODO
  130. public static String[] removeConsecutiveDuplicates(String[] array) {
  131. String prevString = null;
  132. int consecDupCount = getConsecDupCount(array);
  133. String[] removedArr = new String[array.length - consecDupCount];
  134. int position = 0;
  135. for (int i = 0; i < array.length; i++) {
  136. if (!array[i].equals(prevString)){
  137. removedArr[position] = array[i];
  138. prevString = array[i];
  139. position++;
  140. }
  141. }
  142. return removedArr;
  143. }
  144. /**
  145. * @param array array of chars
  146. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  147. */ // TODO
  148. public static String[] packConsecutiveDuplicates(String[] array) {
  149. int consecDupCount = getConsecDupCount(array);
  150. String[] packedArr = new String[array.length - consecDupCount];;
  151. String prevString = array[0];
  152. packedArr[0] = array[0];
  153. int position = 0;
  154. for (int i = 1; i < array.length; i++) {
  155. if (array[i].equals(prevString)) {
  156. packedArr[position] += prevString;
  157. }
  158. else {
  159. position++;
  160. packedArr[position] = array[i];
  161. prevString = array[i];
  162. }
  163. }
  164. return packedArr;
  165. }
  166. private static int getConsecDupCount(String[] array) {
  167. String prevString = null;
  168. int count = 0;
  169. for (String string : array) {
  170. if (string.equals(prevString)){
  171. count++;
  172. }
  173. prevString = string;
  174. }
  175. return count;
  176. }
  177. }