StringArrayUtils.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import java.util.*;
  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(String x : array) {
  41. if(value.equals(x)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * @param array of String objects
  49. * @return an array with identical contents in reverse order
  50. */ // TODO
  51. public static String[] reverse(String[] array) {
  52. String[] reverseArr = new String[array.length];
  53. for(int i = 0; i < array.length; i++){
  54. reverseArr[i] = array[array.length - i - 1];
  55. }
  56. return reverseArr;
  57. }
  58. /**
  59. * @param array array of String objects
  60. * @return true if the order of the array is the same backwards and forwards
  61. */ // TODO
  62. public static boolean isPalindromic(String[] array) {
  63. String[] reverseArr = reverse(array);
  64. String[] regArr = new String[array.length];
  65. for(int i= 0; i < array.length; i++){
  66. regArr[i] = array[i];
  67. if(regArr[i] == reverseArr[i]) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * @param array array of String objects
  75. * @return true if each letter in the alphabet has been used in the array
  76. */ // TODO
  77. public static boolean isPangramic(String[] array) {
  78. String alphabet = "abcdefghijklmnopqrstuvwxyz";
  79. String fullCombinedString = String.join("", array).toLowerCase();
  80. for(char c : alphabet.toCharArray()) {
  81. if(fullCombinedString.indexOf(c) < 0) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. /*
  87. for(char c : alphabet.toCharArray()) {
  88. boolean found = false;
  89. for(String word : array) {
  90. word = word.toLowerCase();
  91. if(word.indexOf(c) >= 0)
  92. found = true;
  93. break;
  94. }
  95. if(!found) {
  96. return false;
  97. }
  98. }
  99. return true;*/
  100. }
  101. /**
  102. * @param array array of String objects
  103. * @param value value to check array for
  104. * @return number of occurrences the specified `value` has occurred
  105. */ // TODO
  106. public static int getNumberOfOccurrences(String[] array, String value) {
  107. int count = 0;
  108. for(String s : array) {
  109. if(s == value) {
  110. count++;
  111. }
  112. }
  113. return count;
  114. }
  115. /**
  116. * @param array array of String objects
  117. * @param valueToRemove value to remove from array
  118. * @return array with identical contents excluding values of `value`
  119. */ // TODO
  120. public static String[] removeValue(String[] array, String valueToRemove) {
  121. List<String> newArray = new ArrayList<>();
  122. for(String s : array) {
  123. if(s != valueToRemove) {
  124. newArray.add(s);
  125. }
  126. }
  127. return newArray.toArray(new String[] {});
  128. }
  129. /**
  130. * @param array array of chars
  131. * @return array of Strings with consecutive duplicates removes
  132. */ // TODO
  133. public static String[] removeConsecutiveDuplicates(String[] array) {
  134. List<String> newArray = new ArrayList<>();
  135. for(int i = 0; i < array.length; i++) {
  136. String added = array[i];
  137. newArray.add(added);
  138. int toRemove = 0;
  139. for(; toRemove + i + 1 < array.length; toRemove++) {
  140. if(array[i + 1 + toRemove] != added){
  141. break;
  142. }
  143. }
  144. i += toRemove;
  145. }
  146. return newArray.toArray(new String[] {});
  147. }
  148. /**
  149. * @param array array of chars
  150. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  151. */ // TODO
  152. public static String[] packConsecutiveDuplicates(String[] array) {
  153. List<String> newArray = new ArrayList<>();
  154. for(int i = 0; i < array.length; i++) {
  155. String added = array[i];
  156. String toAdd = added;
  157. int toRemove = 0;
  158. for(; toRemove + i + 1 < array.length; toRemove++) {
  159. if(array[i + 1 + toRemove] != added){
  160. break;
  161. }
  162. toAdd += array[i + 1 + toRemove];
  163. }
  164. newArray.add(toAdd);
  165. i += toRemove;
  166. }
  167. System.out.println(Arrays.toString(newArray.toArray()));
  168. return newArray.toArray(new String[] {});
  169. }
  170. }