diff --git a/pom.xml b/pom.xml
index d10c35e..991cf63 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,18 @@
com.zipcodewilmington.labsarrayutils1.0-SNAPSHOT
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ 11
+
+
+
+ junit
diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java
index 4bcce66..e6dbbcc 100644
--- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java
+++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java
@@ -1,5 +1,7 @@
package com.zipcodewilmington;
+import java.util.*;
+
/**
* Created by leon on 1/29/18.
*/
@@ -17,7 +19,7 @@ public static String getFirstElement(String[] array) {
* @return second element in specified array
*/
public static String getSecondElement(String[] array) {
- return array[1];
+ return array[1];
}
/**
@@ -25,7 +27,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 +36,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,7 +45,8 @@ public static String getSecondToLastElement(String[] array) {
* @return true if the array contains the specified `value`
*/ // TODO
public static boolean contains(String[] array, String value) {
- return false;
+ List arrayList = new ArrayList(Arrays.asList(array));
+ return arrayList.contains(value);
}
/**
@@ -50,7 +54,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 [] revArray = new String[array.length];
+ int newArray = 0;
+ for (int i = array.length - 1; i >= 0; i--) {
+ revArray[newArray] = array[i];
+ newArray++;
+ }
+ return revArray;
}
/**
@@ -58,7 +68,8 @@ 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[] revArray = reverse(array);
+ return Arrays.equals(revArray, array);
}
/**
@@ -66,7 +77,16 @@ 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 pangramic = Arrays.toString(array);
+ pangramic = pangramic.toLowerCase(Locale.ROOT);
+ if (pangramic.length() < 26) {
+ return false;
+ } else
+ for (char i = 'a'; i <= 'z'; i++) {
+ if (pangramic.indexOf(i) == -1)
+ return false;
+ }
+ return true;
}
/**
@@ -75,7 +95,9 @@ 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;
+ List arrayList = new ArrayList(Arrays.asList(array));
+ return Collections.frequency(arrayList,value); //collections holds different methods I can use
+
}
/**
@@ -84,7 +106,11 @@ 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 arrayList = new ArrayList(Arrays.asList(array));
+ while (arrayList.contains(valueToRemove)) {
+ arrayList.remove(valueToRemove);
+ }
+ return arrayList.toArray(new String [0]);
}
/**
@@ -92,7 +118,18 @@ 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;
+
+ List arrayList = new ArrayList();
+ String previousElement = "";
+ for (String element : array) {
+ if (element != previousElement) { //if the current element is not equal to the previous then add the element, this will pass over the elemets that are the same
+ arrayList.add(element);
+ }
+ previousElement = element;
+ }
+
+
+ return arrayList.toArray(new String[0]);
}
/**
@@ -100,7 +137,19 @@ 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;
+ int lastIndex = 0;
+ List arrayList = new ArrayList ();
+ arrayList.add(array[0]); //start at index 0
+ for (int i = 1; i < array.length; i++) { // this will start looping at the 1st index that does not have the same character
+ if (arrayList.get(lastIndex).contains(array[i])) { // this is saying if the last index has the current character
+ arrayList.set(lastIndex, (arrayList.get(lastIndex) + array[i])); // will add the duplicate letter to the previous letter
+ }
+ else { // if they are not the same, nothing with be packed together because we are missing duplicates
+ lastIndex++; // go to the next index and have that non duplicate letter start here
+ arrayList.add(array[i]);
+ }
+ }
+ return arrayList.toArray(new String[0]); // return string array
}