123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. import java.util.Arrays;
  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 answer = false;
  41. for(String arr: array) {
  42. if (arr.equals(value)){
  43. answer = true;
  44. break;
  45. }
  46. }
  47. return answer;
  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[] answer = new String[array.length];
  55. int count = 0;
  56. for(int i = array.length - 1; i >= 0; i--){
  57. answer[count] = array[i];
  58. count++;
  59. }
  60. return answer;
  61. }
  62. /**
  63. * @param array array of String objects
  64. * @return true if the order of the array is the same backwards and forwards
  65. */ // TODO
  66. public static boolean isPalindromic(String[] array) {
  67. boolean answer = false;
  68. String[] flipped = reverse(array);
  69. if( Arrays.equals(array, flipped)){
  70. answer = true;}
  71. return answer;
  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[] alpha = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
  79. String bigString = "";
  80. boolean result = true;
  81. //could be seperate method makeString(String[] arr)
  82. for(int i = 0; i<array.length;i++){
  83. bigString = bigString + array[i];
  84. }
  85. // could be seperate method backToArray(String string)
  86. String[] arrBig = new String[bigString.length()];
  87. for(int j = 0; j < arrBig.length; j++){
  88. arrBig[j]= (bigString.charAt(j)+"").toLowerCase();
  89. }
  90. for(int i = 0; i < alpha.length; i++){
  91. if(!contains(arrBig,alpha[i])){
  92. result = false;
  93. break;
  94. }
  95. }
  96. return result;
  97. }
  98. /**
  99. * @param array array of String objects
  100. * @param value value to check array for
  101. * @return number of occurrences the specified `value` has occurred
  102. */ // TODO
  103. public static int getNumberOfOccurrences(String[] array, String value) {
  104. int count = 0;
  105. for(int i = 0; i < array.length; i++){
  106. if(array[i].contains(value)){
  107. count++;
  108. }
  109. }
  110. return count;
  111. }
  112. /**
  113. * @param array array of String objects
  114. * @param valueToRemove value to remove from array
  115. * @return array with identical contents excluding values of `value`
  116. */ // TODO
  117. public static String[] removeValue(String[] array, String valueToRemove) {
  118. //iterate over an array of strings
  119. // if array[i] !contains valueToRemove, add to new string
  120. // convert string to array
  121. // return new array
  122. String saveMe = "";
  123. for(int i = 0; i < array.length; i++){
  124. if(array[i] != valueToRemove){
  125. saveMe = saveMe + array[i] + " ";
  126. }
  127. }
  128. String[] answer = saveMe.split(" ", 0);
  129. return answer;
  130. }
  131. /**
  132. * @param array array of chars
  133. * @return array of Strings with consecutive duplicates removes
  134. */ // TODO
  135. public static String[] removeConsecutiveDuplicates(String[] array) {
  136. //create string to save needed values
  137. int count = 0;
  138. String saveMe = "";
  139. //loop array and compare array[i] and array[i+1]
  140. for(int i = 0; i < array.length; i++){
  141. if(count > 1 && array[i] == array[i+1]){
  142. saveMe = saveMe;
  143. } else if(count == 0 && array[i] == array[i + 1] && i < array.length - 2){
  144. count ++;
  145. saveMe = saveMe + array[i] + " ";
  146. }else if(array[i] != array[i -1]){
  147. saveMe = saveMe + array[i] + " ";
  148. }
  149. }
  150. String[] answer = saveMe.split(" ");
  151. return answer;
  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 counter = getCount(array);
  159. String [] answer = new String[array.length - counter];
  160. String previous = array[0];
  161. answer[0] = array[0];
  162. int index = 0;
  163. for(int i = 1; i < array.length; i++) {
  164. if(array[i].equals(previous)){
  165. answer[index] += array[i];
  166. }else{
  167. index++;
  168. answer[index] = array[i];
  169. previous = array[i];
  170. }
  171. }
  172. return answer;
  173. }
  174. public static int getCount (String[] array) {
  175. int count = 0;
  176. for(int i = 0; i < array.length - 1; i++){
  177. if(array[i].equals(array[i + 1])){
  178. count++;
  179. }
  180. }
  181. return count;
  182. }
  183. }