Choose Your Language

Monday 15 July 2013

java program to delete duplicate entry in an array

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class DeleteDuplicate {

    static int[] a = {5,1,2,3,4,5,7,8,9,10};
    static int[] b = new int[10];
    static int f, i, k, j = 0;
    static int l = a.length;

    static void DeleteElementInt(int elementToDelete) {
        j = 0;
        for (int i = 0; i < l; i++)
            if (a[i] != elementToDelete){
                b[i - j] = a[i];}
            else{
                ++j;
        }
    }
static  int findDuplicate(){
    int num = 0;
    int best = 0;
    for (int i = 0; i < a.length; i++) {
    if (a[i] > best) {
        num = i;
        best = a[i];
    }   return best;
    }
    return best;
}

    public static void main(String[] args) {
       
    System.out.println("Array elements are:");
        for (i = 0; i < a.length; i++)
            System.out.print(a[i]+" ");
        InputStreamReader is = new InputStreamReader(System.in);
       System.out.println("");
       int duplicate=findDuplicate();
       System.out.println("Duplicate entry is "+duplicate);
            DeleteElementInt(duplicate);
            System.out.println("New array:");
            for (i = 0; i < l - j; i++)
                System.out.print(b[i]+" ");
           }
}
   
output:
Array elements are:
5 1 2 3 4 5 7 8 9 10
Duplicate entry is 5
New array:
1 2 3 4 7 8 9 10

2 comments:

  1. thanks for this codes...It helps a lot...need to solve it first..

    ReplyDelete
  2. You can also check here
    http://javaarcade.blogspot.in/2013/10/program-to-remove-duplicates-from-array_9265.html

    ReplyDelete