Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 61 additions & 12 deletions StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Created by Yang on 1/23/20.
Expand All @@ -9,31 +13,31 @@ public class StringArrayUtils {
* @return first element of specified array
*/ // TODO
public static String getFirstElement(String[] array) {
return null;
return (array == null || array.length == 0) ? null : array[0];
}

/**
* @param array array of String objects
* @return second element in specified array
*/
public static String getSecondElement(String[] array) {
return null;
return (array == null || array.length == 0) ? null : array[1];
}

/**
* @param array array of String objects
* @return last element in specified array
*/ // TODO
public static String getLastElement(String[] array) {
return null;
return (array == null || array.length == 0) ? null : array[array.length - 1];
}

/**
* @param array array of String objects
* @return second to last element in specified array
*/ // TODO
public static String getSecondToLastElement(String[] array) {
return null;
return (array == null || array.length == 0) ? null : array[array.length - 2];
}

/**
Expand All @@ -42,6 +46,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) {
if (array == null || array.length == 0) return false;
for (String s : array) if (s.equals(value)) return true;
return false;
}

Expand All @@ -50,23 +56,43 @@ 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;
if (array == null || array.length < 2) return array;
int i = 0, j = array.length - 1;
while (i < j) {
String tmp = array[i];
array[i++] = array[j];
array[j--] = tmp;
}
return array;
}

/**
* @param array array of String objects
* @return true if the order of the array is the same backwards and forwards
*/ // TODO
public static boolean isPalindromic(String[] array) {
return false;
if (array == null || array.length == 0) return false;
int i = 0, j = array.length - 1;
while (i < j) if (!array[i++].equals(array[j--])) 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;
if (array == null || array.length == 0) return false;
Set<Character> set = new HashSet<>();
for (char c = 'a'; c <= 'z'; c++) {
set.add(c);
}
for (String s : array) {
for (char c : s.toCharArray()) {
set.remove(Character.toLowerCase(c));
}
}
return set.isEmpty();
}

/**
Expand All @@ -75,7 +101,10 @@ 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;
if (array == null || array.length == 0) return 0;
int result = 0;
for (String s : array) if (s.equals(value)) result++;
return result;
}

/**
Expand All @@ -84,23 +113,43 @@ 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;
return Arrays.stream(array).filter(e -> !e.equals(valueToRemove)).toArray(String[] :: new);
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
if (array == null || array.length < 2) return array;
int i = 1;
for (int j = 1; j < array.length; j++) {
if (array[j] != array[i - 1]) {
array[i++] = array[j];
}
}
String[] result = new String[i];
for (int j = 0; j < i; j++) result[j] = array[j];
return result;
}

/**
* @param array array of chars
* @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;
if (array == null || array.length < 2) return array;
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i != 0 && !array[i].equals(array[i - 1])) {
result.add(sb.toString());
sb.delete(0, sb.length());
}
sb.append(array[i]);
}
result.add(sb.toString());
return result.toArray(new String[0]);
}


Expand Down