StringArrayUtils.java 5.4KB

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