diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..1738b9f 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,8 @@ package com.zipcodewilmington; +import java.util.ArrayList; +import java.util.Arrays; + /** * Created by leon on 1/29/18. */ @@ -25,7 +28,8 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + int last = array.length - 1; + return array[last]; } /** @@ -33,7 +37,8 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + int secondToLast = array.length-2; + return array[secondToLast]; } /** @@ -42,6 +47,9 @@ 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++){ + if(array[i]==value){return true;} + } return false; } @@ -50,7 +58,13 @@ 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; + String[] ans = new String[array.length]; + int j = 0; + for (int i = array.length - 1; i >= 0; i--){ + ans[j] = array[i]; + j++; + } + return ans; } /** @@ -58,7 +72,14 @@ 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; + int i = 0; + int j = array.length - 1; + + for(i = 0; i < j; i++){ + if(array[i] == array[j]) {j--;} + else return false; + } + return true; } /** @@ -66,7 +87,14 @@ 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 sentence = String.join("",array).toLowerCase(); + + String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; + for (int i = 0; i <26; i++){ + if (sentence.contains(alphabet[i])){} + else {return false;} + } + return true; } /** @@ -75,7 +103,11 @@ 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 ans = 0; + for (int i = 0; i < array.length; i++){ + if(array[i] == value){ans++;} + } + return ans; } /** @@ -84,7 +116,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; + ArrayList arrayListed = new ArrayList(Arrays.asList(array)); + arrayListed.remove(valueToRemove); + String[] ans = arrayListed.toArray(new String[0]); + return ans; } /** @@ -92,7 +127,19 @@ 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 arrayListed = new ArrayList(Arrays.asList(array)); + //System.out.println(array); + //System.out.println(arrayListed); + for (int i = 0; i < arrayListed.size()-1; i++) { + if (arrayListed.get(i) == arrayListed.get(i + 1)) { + arrayListed.remove(i + 1); + i--; + } + } + // System.out.println(arrayListed); + String[] ans = arrayListed.toArray(new String[0]); + //System.out.println(ans); + return ans; } /** @@ -100,7 +147,16 @@ public static String[] removeConsecutiveDuplicates(String[] array) { * @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; + String joinerLucas = array[0]; + for (int i = 1; i < array.length; i++){ + if (array[i]==array[i-1]){ + joinerLucas += array[i]; + } else { + joinerLucas = joinerLucas + "%%%" + array[i]; + } + } + String[] ans = joinerLucas.split("%%%"); + return ans; }