Skip to main content
public static <T> T findPrevailingElement(
    Stream<T> ids) {
    State s = new State (); // s.total is 0
    Map<T, Long> counters = ids
        .peek(e -> ++s.total)
        .collect(Collectors.groupingBy(
            Function.identity(), <YOUR_INPUT>));
    for (Entry<T, Long> element : counters.entrySet()) {
        if (element.getValue() >= (s.total + 1) / 2) {
            return element.getKey();
        }
    }
    throw new IllegalStateException(
       "no element occurs more than 50");
}

Meine Lösung

  • Dieses Feld dient zur Validierung und sollte nicht verändert werden.

Code gut. Alles gut.

Leave a Reply