import java.util.*; /** * Template code to help you parse the standard input * according to the problem statement. **/ class Solution { public static void main( String[] args ) { // hint: read values via Scanner methods var scanner = new Scanner( System.in ); // code your solution here Map lastOccurrences = new HashMap<>(); int startIndex = 0; int maxLength = 0; int index = 0; while ( scanner.hasNext() ) { Integer previousOccurrence = lastOccurrences.put( scanner.next(), index++ ); if ( previousOccurrence != null ) { if ( previousOccurrence >= startIndex ) { maxLength = Math.max( maxLength, index - 1 - startIndex ); startIndex = previousOccurrence + 1; } } } maxLength = Math.max( maxLength, index - startIndex ); // Write result with System.out.println() System.out.println( maxLength ); } }