Write a java program to find the largest span in the array

Write a program to read an integer array, find the largest span in the array.
Span is the count of all the elements between two repeating elements including the repeated elements.

The return type is an integer.

Input and Output Format:
Input consists of an integer n which is the number of elements followed by n integer values.
The output consists of an integer.
Refer sample output for formatting specifications.

 

Sample Input 1:
6
4
2
1
4
5
7
Sample Output 1:
4

java program to find the largest span in the array

import java.util.Scanner;
 
public class Main {
 
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
 
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print(display(a,n));
}
 
public static int display(int[] x,int n)
{
int gap=0,max=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(x[i]==x[j])
{
gap=j;
}
}
if(gap-i>max)
max=gap-i;
 
}
return max+1;
}
 
}