import java.util.Arrays; import java.io.*; import java.util.*; import java.lang.StringBuilder; /** * Created by leon on 1/29/18. */ public class StringArrayUtils { /** * @param array array of String objects * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { return array[0]; } /** * @param array array of String objects * @return second element in specified array */ public static String getSecondElement(String[] array) { return array[1]; } /** * @param array array of String objects * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { return array[array.length - 1]; } /** * @param array array of String objects * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { return array[array.length - 2]; } /** * @param array array of String objects * @param value value to check array for * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { boolean contains = false; for(String element : array){ if(element.equals(value)){ contains = true; } } return contains; } /** * @param array of String objects * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { String[] reverse = new String[array.length]; for (int j=array.length-1, i=0; j>=0; j--, i++){ reverse[i] = array[j]; } return reverse; } /** * @param array array of String objects * @return true if the order of the array is the same backwards and forwards */ // TODO public static boolean isPalindromic(String[] array) { boolean isPalindromic = false; String[] reverse = reverse(array); if (Arrays.equals(array,reverse)){ isPalindromic = true; } return isPalindromic; } /** * @param array array of String objects * @return true if each letter in the alphabet has been used in the array */ // TODO public static boolean isPangramic(String[] array) { String arrayString = Arrays.toString(array).toLowerCase(); if(arrayString.length() < 26){ return false; } for (char c = 'a'; c <='z'; c++){ if(arrayString.indexOf(c) < 0){ return false; } } return true; } /** * @param array array of String objects * @param value value to check array for * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { int numberOfOccurrences = 0; for (String element : array){ if(element.equals(value)){ numberOfOccurrences += 1; } } return numberOfOccurrences; } /** * @param array array of String objects * @param valueToRemove value to remove from array * @return array with identical contents excluding values of `value` */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove); String[] newArray = new String[array.length - numberOfOccurrences]; int i = 0; for(String element : array){ if(!element.equals(valueToRemove)){ newArray[i] = element; i++; } } return newArray; } public static int numberOfConsecutiveDuplicates(String[] array) { int numberOfConsecDups = 0; for(int i=1; i<=array.length-1; i++){ if(array[i].equals(array[i-1])){ numberOfConsecDups += 1; } } return numberOfConsecDups; } /** * @param array array of chars * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { ArrayList newList = new ArrayList(); //add first element of array to new array newList.add(array[0]); //compare each element in your array starting with the second for (int i=1; i<=array.length-1; i++){ //if an element does not equal an element before it, add to new array if(!(array[i].equals(array[i-1]))){ newList.add(array[i]); } } return newList.toArray(new String[0]); } /** * @param array array of chars * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings */ // TODO public static String[] packConsecutiveDuplicates(String[] array) { //create a new string using string builder StringBuilder arrayAsString = new StringBuilder(); //add first element in array to new array string arrayAsString.append(array[0]); //look at each element in the array for(int i=1; i<=array.length-1; i++){ //if the element in the array equals the previous element, add it to // the string if(array[i].equals(array[i-1])){ arrayAsString.append(array[i]); } else { //if the element in the array does not equal the previous element, // add it to the string after a space arrayAsString.append(" "); arrayAsString.append(array[i]); } } //convert the new string to a space split string array String[] packConsecDupArray = arrayAsString.toString().split(" "); return packConsecDupArray; } }