Tuesday, 15 October 2013

Java program to remove duplicates from a character array .

Here is the code ....



       

public class P13 {
 public static void main(String args[]) {
  char[] chr = {
   'g',
   'a',
   'g',
   'r',
   'p',
   'k',
   't',
   'k',
   'r',
   's'
  };
  boolean[] boo = new boolean[256];
  int cal = 0;
  for (int i = 0, val; i < chr.length; i++) {

   if (i < chr.length - cal) {

    val = chr[i];
    if (boo[val]) {
     cal = cal + 1;

     for (int k = i; k < chr.length - 1; k++) {
      chr[k] = chr[k + 1];
     }
     chr[chr.length - 1] = '\0';
     i = i - 1;
    } else boo[val] = true;
    //System.out.println("hello2");

   } else {
    break;
   }
  }

  for (int h = 0; h < chr.length; h++)
   System.out.print(chr[h]);

 }
}

       
 

No comments:

Post a Comment