123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. return Arrays.asList(array).contains(value);
  41. }
  42. /**
  43. * @param array of String objects
  44. * @return an array with identical contents in reverse order
  45. */ // TODO
  46. public static String[] reverse(String[] array) {
  47. int length = array.length;
  48. String[] result = new String[length];
  49. for (int i = 0; i < length; i++) {
  50. result[i] = array[(length - 1) - i];
  51. }
  52. return result;
  53. }
  54. /**
  55. * @param array array of String objects
  56. * @return true if the order of the array is the same backwards and forwards
  57. */ // TODO
  58. public static boolean isPalindromic(String[] array) {
  59. return Arrays.equals(array, reverse(array));
  60. }
  61. /**
  62. * @param array array of String objects
  63. * @return true if each letter in the alphabet has been used in the array
  64. */ // TODO
  65. public static boolean isPangramic(String[] array) {
  66. String[] alpha = {"a","b","c","d","e","f","g","h","i","j",
  67. "k","l","m","n","o","p","q","r","s","t",
  68. "u","v","w","x","y","z"};
  69. String s = "";
  70. int count = 0;
  71. for (String e : array) {
  72. s += e.replace(" ", "");
  73. }
  74. String[] check = s.toLowerCase().split("");
  75. for (String letter : alpha) {
  76. if (contains(check, letter)) count++;
  77. }
  78. return count == alpha.length;
  79. }
  80. /**
  81. * @param array array of String objects
  82. * @param value value to check array for
  83. * @return number of occurrences the specified `value` has occurred
  84. */ // TODO
  85. public static int getNumberOfOccurrences(String[] array, String value) {
  86. int count = 0;
  87. for (String i : array) {
  88. count += (i == value) ? 1 : 0;
  89. }
  90. return count;
  91. }
  92. /**
  93. * @param array array of String objects
  94. * @param valueToRemove value to remove from array
  95. * @return array with identical contents excluding values of `value`
  96. */ // TODO
  97. public static String[] removeValue(String[] array, String valueToRemove) {
  98. ArrayList<String> result = new ArrayList<>();
  99. for (String s : array) {
  100. if (!s.equals(valueToRemove)) result.add(s);
  101. }
  102. return result.toArray(new String[result.size()]);
  103. }
  104. /**
  105. * @param array array of chars
  106. * @return array of Strings with consecutive duplicates removes
  107. */ // TODO
  108. public static String[] removeConsecutiveDuplicates(String[] array) {
  109. ArrayList<String> result = new ArrayList<>();
  110. for (int i = 0; i < array.length - 1; i++) {
  111. if (!array[i].equals(array[i + 1])) result.add(array[i]);
  112. }
  113. result.add(array[array.length - 1]);
  114. return result.toArray(new String[result.size()]);
  115. }
  116. /**
  117. * @param array array of chars
  118. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  119. */ // TODO
  120. public static String[] packConsecutiveDuplicates(String[] array) {
  121. ArrayList<String> result = new ArrayList<>();
  122. StringBuilder addMe = new StringBuilder();
  123. for (int i = 0; i < array.length - 1; i++) {
  124. if (!array[i].equals(array[i + 1])) {
  125. addMe.append(array[i]);
  126. result.add(addMe.toString());
  127. addMe = new StringBuilder();
  128. }
  129. else {
  130. addMe.append(array[i]);
  131. }
  132. }
  133. addMe.append(array[array.length - 1]);
  134. result.add(addMe.toString());
  135. return result.toArray(new String[result.size()]);
  136. }
  137. }