/** * Created by leon on 1/29/18. */ import java.util.Arrays; public class StringArrayUtils { /** * @param array array of String objects * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { String result = ""; for(int i = 0; i < array.length; i++) { result += array[0]; break; } return result; } /** * @param array array of String objects * @return second element in specified array */ public static String getSecondElement(String[] array) { String result = ""; for(int i = 0; i < array.length; i++) { result += array[1]; break; } return result; } /** * @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 hasString = false; for(String s : array) { if(s.equals(value)) { hasString = true; break; } } return hasString; } /** * @param array of String objects * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { String[] ns = new String[array.length]; for(int i = 0; i < array.length; i++){ ns[array.length -1 -i] = array[i]; } return ns; } /** * @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 ispalindromin = false; for(int i = 0; i < array.length / 2; i++) { if(array[i] == array[array.length -i -1]) { ispalindromin = true; } } return ispalindromin; } /** * @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) { boolean ispangramic = false; for(char i = 'A'; i <= 'Z'; i++) { if(Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i + 32 ) < 0)) { ispangramic = false; break; } else { ispangramic = true; } } return ispangramic; } /** * @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 count = 0; for(String s : array){ if(s.equals(value)){ count +=1; } } return count; } /** * @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 count = 0; int occurences = getNumberOfOccurrences(array, valueToRemove); String[] str = new String[array.length - occurences]; for(String element : array) { if(element != valueToRemove) { str[count] = element; count++; } } return str; } /** * @param array array of chars * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { int dupCount = 0; for(int i = 0; i < array.length -1; i++) { if(array[i].equals(array[i+1])) { dupCount = dupCount + 1; } } String[] str = new String[array.length - dupCount]; int j = 0; for(int i = 0; i < array.length -1; i++) { if(!array[i].equals(array[i + 1])) { str[j++] = (array[i]); } } str[str.length -1] = array[array.length -1]; return str; } /** * @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) { int dupCount = 0; for(int i = 0; i < array.length-1; i++){ if(array[i].equals(array[i+1])){ dupCount = dupCount + 1; } } String[] str = new String[array.length - dupCount]; int j = 0; for(int i = 0; i < array.length-1; i++){ if(array[i].equals(array[i+1])){ StringBuilder sb = new StringBuilder(array[i]); sb.append(array[i+1]); str[j++] = (array[i]); } } return str; } }