diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..f3ebd00 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,10 @@ package com.zipcodewilmington; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + /** * Created by leon on 1/29/18. */ @@ -25,7 +30,7 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + return array[array.length - 1]; //return value of array at end of arrays length minus 1 } /** @@ -33,7 +38,7 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + return array[array.length - 2]; } /** @@ -42,6 +47,15 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + for (int i = 0; i < array.length; i++) { //itar through array using .contains() searching for given value + String s = array[i]; + if (array[i].contains(value)) { + return true; + } + + } + + return false; } @@ -50,7 +64,10 @@ 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 = new ArrayList(); + Collections.addAll(list, array); + Collections.reverse(list); + return list.toArray(new String[0]); } /** @@ -58,7 +75,10 @@ public static String[] reverse(String[] array) { * @return true if the order of the array is the same backwards and forwards */ // TODO public static boolean isPalindromic(String[] array) { - return false; + String[] reversedArr = reverse(array); + return Arrays.equals(reversedArr, array); + + } /** @@ -66,7 +86,17 @@ public static boolean isPalindromic(String[] array) { * @return true if each letter in the alphabet has been used in the array */ // TODO public static boolean isPangramic(String[] array) { - return false; + String x = Arrays.toString(array); + x = x.toLowerCase(); + if (x.length() < 26) { + return false; + } else + for (char i = 'a'; i <= 'z'; i++) { + if (x.indexOf(i) == -1) + return false; + } + return true; + } /** @@ -75,7 +105,16 @@ 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 count = 0; + for (int i = 0; i < array.length; i++) { + if (array[i] == value) { + count++; + + } + + } + + return count; } /** @@ -84,24 +123,75 @@ 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; + /* int j = 0; + + String[] x = new String[array.length-1]; + for (int i = 0; i < array.length; i++) { + if (array[i] != valueToRemove){ + x[j] = array[i]; + j++; + } + } + return x; */ + + ArrayList arrayList = new ArrayList(Arrays.asList(array)); + while (arrayList.contains(valueToRemove)) { + arrayList.remove(valueToRemove); + } + return arrayList.toArray(new String[0]); //set array to 0 to populate with new arraylist + } + /** * @param array array of chars * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { - return null; + //ArrayList arrayList = new ArrayList(Arrays.asList(array)); + String finalString = ""; + String lastString = ""; + int count = 0; + for (String currentString : array) { + if (currentString == lastString) { + lastString = currentString; + } else if (currentString != lastString) { + finalString += currentString + " "; + lastString = currentString; + count++; + } + } + String[] x = new String[count]; + x = finalString.split(" "); + return x; } /** * @param array array of chars * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings + * String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"}; + * String[] expected = {"tt", "q", "aaa", "b", "cc", "aa", "d", "eee"}; */ // TODO public static String[] packConsecutiveDuplicates(String[] array) { - return null; - } + int length = array.length; + int lastIndex = 0; + ArrayList arrayList = new ArrayList(); + arrayList.add(array[0]); + for (int i = 1; i < length; i++) { + if (arrayList.get(lastIndex).contains(array[i])) { // "aa" != "a" use contains + arrayList.set(lastIndex, (arrayList.get(lastIndex) + array[i])); + } else { + lastIndex++; + arrayList.add(array[i]); //adds non dupes + } + } + return arrayList.toArray(new String[0]); + } } + + + + +