i am getting OutofBounds error in it. The outofbounds is happenning here "if (arr[j]==arr[i])" the program is find the dulicate numbers and make them 0. and to shift all those dulicate numbers left.
input: {10,20,4,20,5}
output:{0,0,10,4,5}
Code:
import java.util.*;
class ArrayWork
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr[],n,i,j,nr=1;
System.out.println("Enter the value for n: ");
n = sc.nextInt();
if(n>25)
System.out.println("Invalid input");
else
{
arr = new int[n];
System.out.println("Enter the array elements: ");
for(i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
for ( i = 0; i < arr.length; i++) {
for (j = i + 1 ; j < arr.length; j++) {
if (arr[j]==arr[i])
nr=arr[i];
for(i=0;i<n;i++)
{
if(arr[i]==nr)
{
for(j=i-1;j>=0&&arr[j]>0;j--)
{
arr[j+1]=arr[j];
}
arr[j+1]=0;
}
}
System.out.println("The array is: ");
for(i=0;i<n;i++)
{
System.out.print(arr[i] + " ");
}
}
}
}
}}