从 HashMap 的一组值中查找模式,通过指示根本没有模式来打破关系

HashMap 看起来像:


Keys: 1, 2, 4


Values: 1, 2, 1

这里的模式是 2,因为它的值是一组值中最高的。


然而,如果


Keys: 1, 2, 4


Values: 1, 2, 2

然后就没有模式了。我得到了我拥有的较小测试用例的预期输出,但不确定这里是否存在不适用于较大输出的逻辑错误/如果值非常大并且不适合整数。


“计数”是 HashMap。


我的代码:


            for (HashMap.Entry<Integer, Integer> entry : count.entrySet()) {

                int curr = entry.getValue();

                if (curr > mode) {

                    mode = curr;

                }

                else if (curr == mode) {

                    mode = -1;

                }

            }


            if (mode != -1) {

                for (HashMap.Entry<Integer, Integer> entry : count.entrySet()) {

                    if (entry.getValue() == mode) {

                        mode = entry.getKey();

                    }

                }

            }


墨色风雨
浏览 91回答 2
2回答

森林海

您的代码不适用于此输入:HashMap<Integer, Integer> count = new HashMap<>();count.put(1, 2);count.put(2, 2);count.put(4, 1);因为您在mode = -1找到重复项后立即进行设置,而不是在最后进行设置。您可以尝试以下代码,该代码在 EntrySet 上进行 2 次传递。一次找到最大值,然后检索具有该最大值的条目。Integer max = count.values().stream()&nbsp; &nbsp; &nbsp; &nbsp; .max(Integer::compareTo)&nbsp; &nbsp; &nbsp; &nbsp; .orElse(-1);List<Integer> maxEntries = count.entrySet().stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(e -> e.getValue().equals(max))&nbsp; &nbsp; &nbsp; &nbsp; .map(Map.Entry::getKey)&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());Integer mode = maxEntries.size() == 1 ? maxEntries.get(0) : -1; //if there is a tie, mode is -1System.out.println(mode);为了能够存储大于 的值Integer,您可以使用Long和BigInteger。

繁星coding

正如 Kartik 已经说过的,您是对的,您的代码中存在错误。最后还有一个循环:一旦找到与该值对应的键,您需要立即跳出它,或者mode可能在以后的迭代中再次更改导致不正确的结果。例如new HashMap<>(Map.of(1, 3, 2, 1))在这种情况下,您的代码将正确确定最高计数为 3。查找相应键的循环将首先将 3 更改为 1,然后将 1 更改为 2。因此结果为 2,这是不正确的。基本问题是您使用相同的变量 ,mode有两个目的,首先是模式的计数,其次是模式的 kay。不要那样做。混淆的风险太高了。我想给出一个解决方案:public static OptionalInt getModeIfUnique(Map<Integer, Integer> count) {&nbsp; &nbsp; Optional<Integer> maxCount = count.values().stream().max(Comparator.naturalOrder());&nbsp; &nbsp; OptionalInt modeKey = maxCount.flatMap(mc -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Map containing only the entries having max count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<Integer, Integer> allModes = new HashMap<>(count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allModes.values().removeIf(c -> c < mc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (allModes.size() == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Optional.of(allModes.keySet().iterator().next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Optional.empty();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(OptionalInt::of)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElse(OptionalInt.empty());&nbsp; &nbsp; return modeKey;}尝试一下:&nbsp; &nbsp; // Kartik’s example - should give no mode&nbsp; &nbsp; System.out.println(getModeIfUnique(Map.of(1, 2, 2, 2, 4, 1)));&nbsp; &nbsp; // My example - should give 1&nbsp; &nbsp; System.out.println(getModeIfUnique(Map.of(1, 3, 2, 1)));&nbsp; &nbsp; // Empty map - should give no mode&nbsp; &nbsp; System.out.println(getModeIfUnique(Map.of()));输出:OptionalInt.emptyOptionalInt[1]OptionalInt.empty
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java