Remove Duplicate Elements from an Array in Java Program

import java.util.*;
class removeduplicate
{
 public static void main(String args[])
 {
  Scanner sc = new Scanner(System.in);
  int n[]=new int[10];
  int i;
  System.out.println("Enter a list of 10 numbers :\n");
  for(i=0;i<10;i++)
  {
   n[i]=sc.nextInt();
  }
  System.out.println("\nRemove duplicate element of a list is as follows:\n");
  System.out.println(n[0]);
  for(i=1;i<=10;i++)
  {
    if(n[i-1]!=n[i])
    System.out.println(n[i]+"\t");
  }
 }
}

Output:

Enter a list of 10 numbers :
11
12
13
13
14
15
16
14
16
16
Remove duplicate element of a list is as follows:
11
12
13
14
15
16
14
16

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at removeduplicate.main(removeduplicate.java:58)

Post a Comment

0 Comments