This method should return the largest group of anagrams in the input array of words, in no particular order. It should return an empty array if there are no anagrams in the input array. This method must call the methods I created that checks if the strings are anagrams method an insertionSort method with a new Comparator class or lambda expression. For some reason when getLargestAnagramGroup gets called on the small array with no anagrams, I fail. I was wondering if I could get some assistance. Thanks!
/**
* @param largestStr a string array.
* @return the largest group of anagrams in the input array of words, in no
* particular order. It returns an empty array if there are no anagrams
* in the input array.
*/
public static String[] getLargestAnagramGroup(String[] largestStr) {
if (largestStr.length == 0) {
return largestStr;
}
// creating the new array
String[] duplicate = new String[largestStr.length];
for (int i = 0; i < duplicate.length; i++) {
duplicate[i] = largestStr[i];
}
// puts everything to lower case and sorts them out
for (int i = 0; i < duplicate.length; i++) {
duplicate[i] = (String) sort(duplicate[i].toLowerCase());
}
// creates a comparator
class compareStrings implements Comparator<String> {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}
compareStrings compString = new compareStrings();
// sorts the list to alphabetical order
insertionSort(duplicate, compString);
// temporary array to hold how many instances of each anagram
ArrayList<Integer> temp = new ArrayList<Integer>();
// compares to each other
String compare = duplicate[0];
// keeps track of the anagram
int totalValues = 0;
// for loop for how many anagrams there are and adds them up
for (int i = 0; i < duplicate.length; i++) {
if (duplicate[i].equals(compare)) {
totalValues++;
} else {
temp.add(totalValues);
compare = duplicate[i];
totalValues = 1;
}
}
// adds the last value to the list
temp.add(totalValues);
int largestNumber = temp.get(0);
// gets the largest number in the array
for (int i = 1; i < temp.size(); i++) {
if (temp.get(i) > largestNumber) {
largestNumber = temp.get(i);
}
}
// will find the correct anagram in the array
int sum = 0;
for (int i = 0; i <= temp.indexOf(largestNumber); i++) {
sum += temp.get(i);
}
String value = duplicate[sum - 1];
String[] largetsAnagram = new String[largestNumber];
// count keeps track of the new list values
int count = 0;
for (int i = 0; i < largestStr.length; i++) {
if (areAnagrams(largestStr[i], value)) {
largetsAnagram[count] = largestStr[i];
count++;
}
}
return largetsAnagram;
}