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