diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..6a57b1b 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,9 @@ package com.zipcodewilmington; + +import java.lang.reflect.Array; +import java.util.*; + /** * Created by leon on 1/29/18. */ @@ -9,6 +13,7 @@ public class StringArrayUtils { * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { + return array[0]; } @@ -17,6 +22,7 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { + return array[1]; } @@ -25,7 +31,8 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + String lastElement = array[array.length - 1]; + return lastElement; } /** @@ -33,7 +40,8 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + String secondToLastElement = array[array.length - 2]; + return secondToLastElement; } /** @@ -41,8 +49,11 @@ public static String getSecondToLastElement(String[] array) { * @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) { - return false; + public static boolean contains(String[] array, String value) { + for (int i = 0; i < value.length(); i++){ + if(array[i] == value); + } + return true; } /** @@ -50,23 +61,61 @@ public static boolean contains(String[] array, String value) { * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { - return null; + List list = Arrays.asList(array); + Collections.reverse(list); + String[] reverseList = list.toArray(array); + return reverseList; } /** * @param array array of String objects * @return true if the order of the array is the same backwards and forwards */ // TODO + + //identify midpoint of an array + //identify last index + //for loop + public static boolean isPalindromic(String[] array) { - return false; + List list = Arrays.asList(array.clone()); + Collections.reverse(list); + String[] reverseArray = list.toArray(new String[array.length]); + for (int i = 0; i < reverseArray.length; i++) { + String thisString = reverseArray[i]; + String thisOtherString = array[i]; + if (!thisString.equals(thisOtherString)) { + return false; + } + } + return true; } + /** * @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) { - return false; + /* convert array of string to one string + nested for loop with original string on outside and alphabet on inside + every time they match copy that to a new array + remove duplicates + if that array matches array of the alphabet + return true + */ + ArrayList stringOfArrays = new ArrayList(Arrays.asList(array)); + String newString = stringOfArrays.toString().toLowerCase(); + String aBCS = "abcdefghijklmnopqrstuvwxyz"; + for (int i = 0; i < aBCS.length(); i++){ + if (newString.indexOf(aBCS.charAt(i)) == -1 ){ + return false; + } + } + + + + + return true; } /** @@ -75,7 +124,12 @@ public static boolean isPangramic(String[] array) { * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { - return 0; + int counter = 0; + for (int i = 0; i < array.length; i++) { + if (value.equals(array[i])) + counter++; + } + return counter; } /** @@ -84,7 +138,10 @@ public static int getNumberOfOccurrences(String[] array, String value) { * @return array with identical contents excluding values of `value` */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { - return null; + List list = new ArrayList(Arrays.asList(array)); + list.remove(valueToRemove); + array = list.toArray(new String[0]); + return array; } /** @@ -92,16 +149,51 @@ public static String[] removeValue(String[] array, String valueToRemove) { * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { - return null; + + ArrayList newArrayList = new ArrayList(); + newArrayList.add(array[0]); + for (int i = 1; i < array.length; i++){ + if(array[i] != array[i - 1]){ + newArrayList.add(array[i]); + } + + } + + + return newArrayList.toArray(new String[newArrayList.size()]); + } + + /** * @param array array of chars - * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings + * @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) { - return null; - } +// iterate through array with for loop counter +// identify if character in string is the same as the next character +// if they are same, concatenate +//add concatenation to new string array + /* StringBuilder stringBuilder = new StringBuilder(arrayToString.charAt(i)).append(arrayToString.charAt(i+1)); + if(stringBuilder.lastIndexOf()) + String newString = stringBuilder.toString(); + String[] stringWithSpaces = newString.split(" "); + array = stringWithSpaces;*/ + ArrayList list = new ArrayList(); + list.add(array[0]); + int counter = 0; + for (int i = 1; i < array.length; i++) { + if(list.get(counter).contains(array[i])){ + list.set(counter, list.get(counter) + array[i]); + }else{ + counter++; + list.add(array[i]); + } + } + return list.toArray(new String[0]); + } }