StringArrayUtils.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import java.util.TreeSet;
  2. import java.util.ArrayList;
  3. /**
  4. * Created by leon on 1/29/18.
  5. */
  6. public class StringArrayUtils {
  7. /**
  8. * @param array array of String objects
  9. * @return first element of specified array
  10. */ // TODO
  11. public static String getFirstElement(String[] array) {
  12. return array[0];
  13. }
  14. /**
  15. * @param array array of String objects
  16. * @return second element in specified array
  17. */
  18. public static String getSecondElement(String[] array) {
  19. return array[1];
  20. }
  21. /**
  22. * @param array array of String objects
  23. * @return last element in specified array
  24. */ // TODO
  25. public static String getLastElement(String[] array) {
  26. return array[array.length-1];
  27. }
  28. /**
  29. * @param array array of String objects
  30. * @return second to last element in specified array
  31. */ // TODO
  32. public static String getSecondToLastElement(String[] array) {
  33. return array[array.length-2];
  34. }
  35. /**
  36. * @param array array of String objects
  37. * @param value value to check array for
  38. * @return true if the array contains the specified `value`
  39. */ // TODO
  40. public static boolean contains(String[] array, String value) {
  41. for(String element:array){
  42. if(element==value){
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * @param array of String objects
  50. * @return an array with identical contents in reverse order
  51. */ // TODO
  52. public static String[] reverse(String[] array) {
  53. String flip="";
  54. for(int i=0;i<array.length/2;i++){
  55. flip=array[i];
  56. array[i]=array[array.length-1-i];
  57. array[array.length-1-i] = flip;
  58. }
  59. return array;
  60. }
  61. /**
  62. * @param array array of String objects
  63. * @return true if the order of the array is the same backwards and forwards
  64. */ // TODO
  65. public static boolean isPalindromic(String[] array) {
  66. int palindrome = array.length;
  67. for(int i=0;i<array.length;i++){
  68. String start = array[i];
  69. String end =array[--palindrome];
  70. if(palindrome<i){
  71. return true;
  72. }
  73. if(start!=end){
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. /**
  80. * @param array array of String objects
  81. * @return true if each letter in the alphabet has been used in the array
  82. */ // TODO
  83. public static boolean isPangramic(String[] array) {
  84. int counter=0;
  85. int i;
  86. /*for(String e:array){
  87. if(e==(i = 'a';i <= 'z'));{
  88. counter++;}}
  89. if(counter<26){
  90. return false;
  91. }*/return true;
  92. }
  93. /**
  94. * @param array array of String objects
  95. * @param value value to check array for
  96. * @return number of occurrences the specified `value` has occurred
  97. */ // TODO
  98. public static int getNumberOfOccurrences(String[] array, String value) {
  99. int counter=0;
  100. for(int i=0;i<array.length;i++){
  101. if(array[i]==value){
  102. counter++;}
  103. }
  104. return counter;
  105. }
  106. /**
  107. * @param array array of String objects
  108. * @param valueToRemove value to remove from array
  109. * @return array with identical contents excluding values of `value`
  110. */ // TODO
  111. public static String[] removeValue(String[] array, String valueToRemove) {
  112. ArrayList<String> remove = new ArrayList<String>();
  113. for(String e:array){
  114. if(!e.equals(valueToRemove)){
  115. remove.add(e);
  116. }
  117. }
  118. return remove.toArray(new String[remove.size()]);
  119. }
  120. /**
  121. * @param array array of chars
  122. * @return array of Strings with consecutive duplicates removes
  123. */ // TODO
  124. public static String[] removeConsecutiveDuplicates(String[] array) {
  125. // for(String e:array){
  126. // if(array[e] !=array[-1]){
  127. // remove.removeAll();
  128. // }
  129. // }
  130. // return remove.toArray(new String[remove.add()]);
  131. //Code below is skipping on index value
  132. // StringBuilder answerString = new StringBuilder();
  133. // for(int i = 1; i < array.length; i++){
  134. // if((array[i] != array[i-1]) && (i < array.length-1)){
  135. // answerString.append(array[i-1] + "`");
  136. // }else if (array[i] != array[i-1]){
  137. // answerString.append(array[i] + "`");
  138. // }
  139. // }
  140. // String answer = answerString.toString();
  141. // String[] answerArray = answer.split("`");
  142. // for(int i = 0; i < answerArray.length; i++){
  143. // System.out.println(answerArray[i]);
  144. // }
  145. // return answerArray;
  146. //loop through code
  147. //compare all digits
  148. //if element appears more than once, count it
  149. //if not, keep it pushing
  150. //ArrayList<String> remove = new ArrayList<String>();
  151. //String getAnswer = null;
  152. //int counter = getCounter(array);
  153. String [] answer = new String[array.length];
  154. int index = 0;
  155. answer[index]=array[0];
  156. index++;
  157. for(int i=1;i<array.length;i++){
  158. if(!answer[index-1].equals(array[i])){
  159. answer[index]=array[i];
  160. index++;
  161. }
  162. }
  163. String [] blank = new String [index];
  164. System.arraycopy(answer, 0, blank, 0, index);
  165. return blank;
  166. }
  167. /**
  168. * @param array array of chars
  169. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  170. */ // TODO
  171. public static String[] packConsecutiveDuplicates(String[] array) {
  172. StringBuilder pack = new StringBuilder(array[0]);
  173. String [] answer = new String[array.length-getCounter(array)];
  174. int index = 0;
  175. for(int i=1;i<array.length;i++){
  176. if(array[i-1].equals(array[i])){
  177. pack.append(array[i]);
  178. }else{
  179. answer[index] = pack.toString();
  180. pack.setLength(0);
  181. pack.append(array[i]);
  182. index++;
  183. }
  184. }
  185. answer[index]=pack.toString();
  186. return answer ;
  187. }
  188. public static int getCounter(String [] array){
  189. int counter = 0;
  190. for(int i=1;i<array.length;i++){
  191. if(array[i].equals(array[i-1])){
  192. counter+=1;
  193. }
  194. }
  195. return counter;
  196. }
  197. }