diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java index 4bcce66..fd276ac 100644 --- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java @@ -1,5 +1,11 @@ package com.zipcodewilmington; +import com.sun.tools.javac.util.ArrayUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * Created by leon on 1/29/18. */ @@ -17,6 +23,7 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { + return array[1]; } @@ -25,7 +32,8 @@ 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]; } /** @@ -33,7 +41,8 @@ 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 +51,11 @@ 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 +64,11 @@ 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[] strArray=new String[array.length]; + for(int i=array.length-1,j=0;i>=0;i--,j++){ + strArray[j]=array[i]; + } + return strArray; } /** @@ -58,7 +76,15 @@ 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[] strArray=new String[array.length]; + strArray=array.clone(); + for(int i= array.length-1,j=0;i>=0;i--,j++){ + if(array[i].equals(strArray[j])){ + continue; + } + else return false; + } + return true; } /** @@ -66,7 +92,26 @@ 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; + Boolean[] flag=new Boolean[26]; + Arrays.fill(flag, false); + String str=Arrays.toString(array).toUpperCase(); + if (str == null) { + return false; + } + System.out.println(str); + int alphabetIndex=0; + for(int i=0;i newList = new ArrayList(); + newList.add(array[0]); + for (int i = 1; i < array.length; i++) { + // Compare current value to previous + if (array[i - 1] != array[i]) { + newList.add(array[i]); + } + } + //to convert list to string array + String[] strArray = newList.toArray(new String[0]); + return strArray; } - /** * @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; + ArrayList newList = new ArrayList(); + int count=0; + newList.add(array[0]); + for (int i = 1; i < array.length; i++) { + // Compare current value to previous + String str=newList.get(count); + if (str.contains((array[i]))) { + newList.set(count,str+array[i]); + //count++; + } + else{ + count++; + newList.add(array[i]); + } + } + //to convert list to string array + return newList.toArray(new String[0]); } - - }