Qn: Write a java program to remove the duplicates in an array?
-By Admin, Last Update On 22nd May,2019 11:46 pm

Program

import java.util.Scanner;
public class RemoveDuplicate{
public static void main(String args[]){
  int  count = 0, i, j;
  int a[]={2,8,3,6,3,5,7,2,6,9,2,8};
  int b[]=new int[a.length];
  for (i=0;i<a.length;i++){
    for (j=0;j<count;j++){
      if(a[i] == b[j]){
        break;
        }
    }
    if (j==count){
      b[count]=a[i];
      count++;
    }
  }
  System.out.println("Array after removing duplicate elements:");
  for (i=0;i<count;i++){
    System.out.print(b[i]+", ");
    }
}
}
Output:
Array after removing duplicate elements:
2
8
3
6
5
7
9

Pgcomments

Comments