public static < T extends Comparable < T > > int isSortedAndUnique ( T [ ] children ) { int r = 0 ; for ( int i = 1 ; i < children . length ; ++ i ) { int c = children [ i - 1 ] . compareTo ( children [ i ] ) ; if ( c == 0 ) { // Duplicate found, though still could be in sorted order. r = 1 ; } else if ( c > 0 ) { // NOT in sorted order return - 1 ; } } // All good return r ; }
Check whether or not the children of this array are sorted according to their underlying order . And if so whether or not there are any duplicate elements encountered .