StringArrayUtils.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. for(int i = 0; i < array.length; i++) {
  41. if(array[i].equals(value)) return true;
  42. }
  43. return false;
  44. }
  45. /**
  46. * @param array of String objects
  47. * @return an array with identical contents in reverse order
  48. */ // TODO
  49. public static String[] reverse(String[] array) {
  50. String[] revArray = new String[array.length];
  51. for (int i = 0; i < array.length; i++) {
  52. revArray[i] = array[array.length - 1 - i];
  53. }
  54. return revArray;
  55. }
  56. /**
  57. * @param array array of String objects
  58. * @return true if the order of the array is the same backwards and forwards
  59. */ // TODO
  60. public static boolean isPalindromic(String[] array) {
  61. for ( int i = 0; i < array.length / 2; i++) {
  62. if (array[i] != array[array.length - 1 - i]) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. /**
  69. * @param array array of String objects
  70. * @return true if each letter in the alphabet has been used in the array
  71. */ // TODO
  72. public static boolean isPangramic(String[] array) {
  73. //boolean result= true;
  74. String words = String.join("", array).toLowerCase();
  75. int counterTrue = 0;
  76. for(char c = 'a';c <= 'z'; c++) {
  77. if(words.contains(String.valueOf(c))) {
  78. //result = false;
  79. counterTrue++;
  80. //return true;
  81. }
  82. }
  83. if (counterTrue == 26) return true;
  84. return false;
  85. }
  86. /**
  87. * @param array array of String objects
  88. * @param value value to check array for
  89. * @return number of occurrences the specified `value` has occurred
  90. */ // TODO
  91. public static int getNumberOfOccurrences(String[] array, String value) {
  92. int num = 0;
  93. for (int i=0;i<array.length;i++){
  94. if (array[i]== value){
  95. num++;
  96. }
  97. }
  98. return num;
  99. }
  100. /**
  101. * @param array array of String objects
  102. * @param valueToRemove value to remove from array
  103. * @return array with identical contents excluding values of `value`
  104. */ // TODO
  105. public static String[] removeValue(String[] array, String valueToRemove) {
  106. int newLength = array.length;
  107. String[] result = new String[array.length-1];
  108. String[] removedValue = new String[1];
  109. int count1 = 0; // count tracks the current index of the output array
  110. int count2 = 0; // count tracks the current index of the output array
  111. for(int i = 0; i < array.length; i++) // i tracks the current index of the input array
  112. {
  113. if(!array[i].contains(valueToRemove)) {
  114. result[count1] = array[i];
  115. count1++;
  116. }else {
  117. removedValue[count2] = array[i];
  118. count2++;
  119. }
  120. }
  121. return result;
  122. }
  123. /**
  124. * @param array array of chars
  125. * @return array of Strings with consecutive duplicates removes
  126. */ // TODO
  127. public static String[] removeConsecutiveDuplicates(String[] array) {
  128. String[] resultArray = new String[array.length];
  129. int resultIndex = 0;
  130. for(int i = 0; i < array.length; i++) {
  131. if(i == 0) {
  132. resultArray[resultIndex] = array[i];
  133. resultIndex++;
  134. }
  135. else if((i > 0 && i < array.length-2) && (!array[i].equals(array[i-1]) || !array[i].equals(array[i+1])))
  136. {
  137. resultArray[resultIndex] = array[i];
  138. resultIndex++;
  139. }else if (i == array.length-1 && !array[i].equals(array[i-1])) {
  140. resultArray[resultIndex] = array[i];
  141. resultIndex++;
  142. }
  143. }
  144. String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
  145. return realAnswer;
  146. }
  147. /**
  148. * @param array array of chars
  149. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  150. */ // TODO
  151. public static String[] packConsecutiveDuplicates(String[] array) {
  152. String[] resultArray = new String[array.length];
  153. StringBuilder dups = new StringBuilder();
  154. int resultIndex = 0;
  155. for(int i = 0; i < array.length; i++) {
  156. if(i == 0) {
  157. resultArray[resultIndex] = array[i];
  158. dups.append(array[i].toString());
  159. resultIndex++;
  160. }
  161. else if((i > 0 && i < array.length-2) && (array[i].equals(array[i-1]) || array[i].equals(array[i+1])))
  162. {
  163. resultArray[resultIndex] = array[i];
  164. dups.append(array[i].toString());
  165. resultIndex++;
  166. }else if (i == array.length-1 && array[i].equals(array[i-1])) {
  167. resultArray[resultIndex] = array[i];
  168. dups.append(array[i].toString());
  169. resultIndex++;
  170. }
  171. }
  172. String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
  173. return realAnswer;
  174. }
  175. }