Link to home
Start Free TrialLog in
Avatar of hadaram49
hadaram49

asked on

java, how to remove dupliates only once from a sorted array?

I am stuck with the program, the program should remove the duplicates only once. for example,
if is there, 11123455533
the result should be, 11234553
what should i do?

import java.io.*;
   import java.util.*;
   import java.lang.*;
   import java.util.HashSet;
   import java.util.Set;
 
 
 
 
 
    class ArrayIns
   {
      private double[] a;  // array of values
      private int nIns;
 
 
       public ArrayIns(int size)
      {
         a = new double[size]; // create a space for the array
 
         nIns++;
 
      }
 
 
 
       public void insert(double value)
      {
         a[nIns] = value;
         nIns++;
      }
 
 
 
 
       public void insertionSort()
      {
         int in, out;
         for(out=1; out<nIns; out++) // out is dividing line
         {
            double temp = a[out]; // remove marked item
            in = out; // start shifts at out
            while(in>0 && a[in-1] >= temp) // until one is smaller,
            {
               a[in] = a[in-1]; // shift item right,
               --in;
            // go left one position
            }
            a[in] = temp; // insert marked item
         }
      } // end for
 
 
       public String toString()
      {
         StringBuffer temp = new StringBuffer();
 
 
         for(double e : a)
            temp.append(e + "");
 
         temp.append("\n");
         return temp.toString();
 
      }
 
 
 
   }
   ////////////------------------main calss --------------//////////////
 
 
 class a3
   {
       public static void main(String[] args)
 
      {
         Set uniques = new HashSet();
         Set dups = new HashSet();
 
 
         ArrayIns a[] = new ArrayIns[3];
         //int[] arr = new int[a];
 
 
         for(int i = 0;  i< 5; i++ )
         {
            Scanner s = new Scanner(System.in);
 
            System.out.println(" Enter the Integers:  ");
 
            a[i] = s.nextInt();
         }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
      }
 
 
   }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ysnky
ysnky
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As ysnky points out, it's easy enough to clean up the array after it has been sorted. If your assignment requires that you modify the sort algorithm itself to eliminate exactly one of a set with population greater than 1, the challenge is quite different.

I think I see where you're going with the two HashSets. It would be easy to track the two sets of information you need with this architecture, but you must reconsider where you need to do this. However, just tracking these items doesn't solve the entire problem: you still have to find a way to get rid of the unwanted elements. Arrays don't cooperate when you want to delete an element, so you'll have to consider alternatives or workarounds that are feasible within the constraints of your assignment. Maybe you can use ysnky's approach or another method more or less like it. Maybe you can use other data types. Maybe you can be circumspect about how you populate an array.

Best of luck.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please pass the complete content of the file as a string to the method getParsedString to get the output.