123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import java.util.ArrayList;
  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 match = false;
  41. for(int i = 1; i < array.length; i++){
  42. if(array[i] == value){
  43. match = true;
  44. break;
  45. } else {
  46. match = false;
  47. }
  48. }
  49. return match;
  50. }
  51. /**
  52. * @param array of String objectsjava object api
  53. *
  54. * @return an array with identical contents in reverse order
  55. */ // TODO
  56. public static String[] reverse(String[] array) {
  57. String temp;
  58. for(int i = 0; i < array.length/2; i++){
  59. temp = array[i];
  60. array[i] = array[array.length-1 -i];
  61. array[array.length -1 -i] = temp;
  62. }
  63. return array;
  64. }
  65. /**
  66. * @param array array of String objects
  67. * @return true if the order of the array is the same backwards and forwards
  68. */ // TODO
  69. public static boolean isPalindromic(String[] array) {
  70. boolean palindrome = false;
  71. for(int i = 0; i < array.length/2; i++){
  72. if( array[i] == array[array.length-1 -i]){
  73. palindrome = true;
  74. }else {
  75. palindrome = false;
  76. }
  77. }
  78. return palindrome;
  79. }
  80. /**
  81. * @param array array of String objects
  82. * @return true if each letter in the alphabet has been used in the array
  83. */ // TODO
  84. public static boolean isPangramic(String[] array) {
  85. boolean pan = false;
  86. StringBuilder build = new StringBuilder();
  87. for (String s: array){
  88. build.append(s);
  89. }
  90. String ofArr = "";
  91. ofArr = build.toString();
  92. for(char i = 'A'; i <='Z'; i++){
  93. if((ofArr.indexOf(i) < 0) && (ofArr.indexOf((char)(i+32))<0)){
  94. pan = false;
  95. }else{
  96. pan = true;
  97. }
  98. }
  99. return pan;
  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 occurrances = 0;
  108. for(String i : array) {
  109. if(value == i){
  110. occurrances++;
  111. }
  112. }
  113. return occurrances;
  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. int index = 0;
  122. int count = getNumberOfOccurrences(array, valueToRemove);
  123. String[] newArr = new String [array.length-count];
  124. for(int i=0; i < array.length; i++){
  125. if (array[i] != valueToRemove) {
  126. newArr[index] = array[i];
  127. index++;
  128. }
  129. }
  130. return newArr;
  131. }
  132. /**
  133. * @param array array of chars
  134. * @return array of Strings with consecutive duplicates removes
  135. */ // TODO
  136. public static String[] removeConsecutiveDuplicates(String[] array) {
  137. String last = null;
  138. int index = 0;
  139. int count = 0;
  140. for(int i = 0; i < array.length -1; i++){
  141. if(array[i].equals(array[i +1])){
  142. count++;
  143. }
  144. }
  145. String[] newArr = new String [array.length-count];
  146. for(int i = 0; i < array.length; i++) {
  147. if (!array[i].equals(last)){
  148. newArr[index]=array[i];
  149. last = array[i];
  150. index++;
  151. }
  152. }
  153. return newArr;
  154. }
  155. /**
  156. * @param array array of chars
  157. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  158. */ // TODO
  159. public static String[] packConsecutiveDuplicates(String[] array) {
  160. int getCounting = getCount(array);
  161. String[] savedArr = new String[array.length-getCounting];
  162. String last = array[0];
  163. savedArr[0] = array[0];
  164. int index = 0;
  165. for(int i = 1; i < array.length; i++){
  166. if(array[i].equals(last)){
  167. savedArr[index] += array[i];
  168. }
  169. else {
  170. index++;
  171. savedArr[index] = array[i];
  172. last = array[i];
  173. }
  174. }
  175. return savedArr;
  176. }
  177. private static int getCount(String[] array){
  178. int count = 0;
  179. for(int i = 0; i < array.length - 1;i++){
  180. if(array[i].equals(array[i + 1])) {
  181. count++;
  182. }
  183. }
  184. return count;
  185. }
  186. }