Solved

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

Posted on 2007-11-18
6
1,656 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:hadaram49
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
ysnky earned 250 total points
ID: 20310646
a dirty code, you can optimise
  private static ArrayList doublicateOnce(int[] ary) {
	  
	  ArrayList list = new ArrayList();
	  if (ary.length > 0) {
		  
		  int active = ary[0];
		  int count  = 0;
		  list.add(new Integer(ary[0]));
		  
		  for (int i=1; i<ary.length; i++) {
		  
			  if (active != ary[i] || count > 0) {
				      				  
				  if (active != ary[i])
					  count = 0;
				  
				  list.add(new Integer(ary[i]));
			  } else {
				  
				  count++;
			  }
			  
			  active = ary[i];
		  }
	  }
	  
	  return list;
  }

Open in new window

0
 
LVL 11

Expert Comment

by:spoxox
ID: 20331092
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.
0
 
LVL 6

Assisted Solution

by:VBGuru
VBGuru earned 250 total points
ID: 20468399
check out the below solution

public class Test {
      public static void main(String[] args) {
            Test test = new Test();
            System.out.println(test.getParsedString("11123455533"));
      }

      private String getParsedString(String stringToParse) {
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            char[] chars = stringToParse.toCharArray();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < chars.length; i++) {
                  if (!map.containsKey(chars[i])) {
                        builder.append(chars[i]);
                        map.put(chars[i], 1);
                  } else if (map.containsKey(chars[i]) && map.get(chars[i]) < 2) {
                        builder.append(chars[i]);
                        map.put(chars[i], 2);
                  }
            }
            return builder.toString();
      }
}
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 20468409
Please pass the complete content of the file as a string to the method getParsedString to get the output.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 52
How do I remove an object from a 3 40
CSV file parsing thru Java 13 34
Problem to Alipay 10 46
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question