@ SafeVarargs public static < T > T mode ( final T ... items ) { if ( ArrayUtils . isNotEmpty ( items ) ) { final HashMap < T , MutableInt > occurrences = new HashMap <> ( items . length ) ; for ( final T t : items ) { final MutableInt count = occurrences . get ( t ) ; if ( count == null ) { occurrences . put ( t , new MutableInt ( 1 ) ) ; } else { count . increment ( ) ; } } T result = null ; int max = 0 ; for ( final Map . Entry < T , MutableInt > e : occurrences . entrySet ( ) ) { final int cmp = e . getValue ( ) . intValue ( ) ; if ( cmp == max ) { result = null ; } else if ( cmp > max ) { max = cmp ; result = e . getKey ( ) ; } } return result ; } return null ; }
Find the most frequently occurring item .