如何 return 数组中出现次数最多的字谜列表?

How to return the list of the highest occurring anagrams within an array?

我有一个问题,关于如何 return 在单个数组的索引中相邻放置的排序字谜列表。澄清这一点:

数组排序的字谜:
sortedAnagrams[0] = asp
sortedAnagrams[1] = pas
sortedAnagrams[2] = 代码
sortedAnagrams[3] = 男女同校
sortedAnagrams[4] = 装饰
sortedAnagrams[5] = 法国

从这个例子中可以清楚地看出,我的索引“2”、“3”和“4”的字谜出现次数最多。如何编写一种方法,让我可以说这些索引包含出现次数最多的字谜?

我开始这样做,但我不知道应该如何继续。

public static String[] getLargestAnagramGroup(String[] stringList){

    for (int i = 0; i < stringList.length; i++) {
        int j = i + 1;
        if (AnagramUtil.areAnagrams(stringList[i],stringList[j]) == true) {
            j++;
        } else {
            i = j;
        } 
    }
    return null; // for now 

public static void main(String[] args) {
    String[] ListOfSortedAnagrams = new String[] {"asp", "pas", "code", "coed", "deco" , "France" }
    System.out.print("Most occurring anagrams are: " + AnagramUtil.getLargestAnagramGroup(String[] ListOfSortedAnagrams));

结果: 最常出现的变位词是:code、coed、deco

您可以规范化字符串,例如对字符串中的字符进行排序并按此规范化值对它们进行分组。

看看:

public class Anagram {

    public static void main(String[] args) {
        String[] listAnagrams = new String[]{"asp", "pas", "code", "coed", "deco", "France"};
        Map<String, List<String>> countMap = new HashMap<>();
        for (String str : listAnagrams) {
            String normalized = normalize(str);
            List<String> strings = countMap.getOrDefault(normalized, new ArrayList<>());
            strings.add(str);
            countMap.put(normalized, strings);
        }
        Optional<Map.Entry<String, List<String>>> max = countMap.entrySet().stream()
                .max(Comparator.comparingInt(entry -> entry.getValue().size()));

        System.out.print("Most occurring anagrams are: " + max.get().getValue());
    }

    private static String normalize(String inputString){
        char[] tempArray = inputString.toCharArray();
        Arrays.sort(tempArray);
        return new String(tempArray);
    }
}

输出:

Most occurring anagrams are: [code, coed, deco]

PS: 可以用stream group重构但是我觉得可读性会差

更新: 这是流组版本:

public class Anagram {

    public static void main(String[] args) {
        String[] listAnagrams = new String[]{"asp", "pas", "code", "coed", "deco", "France"};

        Optional<Map.Entry<String, List<String>>> maxNormalised = Arrays.stream(listAnagrams)
                .collect(Collectors.groupingBy(Anagram::normalize))
                .entrySet().stream()
                .max(Comparator.comparingInt(entry -> entry.getValue().size()));

        System.out.print("Most occurring anagrams are: " + maxNormalised.get().getValue());
    }

    private static String normalize(String inputString){
        char[] tempArray = inputString.toCharArray();
        Arrays.sort(tempArray);
        return new String(tempArray);
    }
}