get first element
while ( count [ index ] == 0 ) index ++ ; let prev = index ; let prevString = sortedArr [ prev ] ; index ++ ; for ( ; index < maxLen ; index ++ ) { if ( count [ index ] != 0 ) { let current = sortedArr [ index ] ; if ( current . includes ( prevString ) ) { prev = index ; prevString = current ; } else { document . write ( " " ) ; return ; } } }