0

I am trying to remove duplicate numbers using method and return non-duplicate numbers, actually I am stuck in method right now. this is my code :

import javax.swing.JOptionPane;
public class duplicateRemover{
     public static void main(String[] args) {
    int[] array = new int[5];

    for (int i=0; i<array.length;i++) {
      String s=JOptionPane.showInputDialog(null,"PLEASE ENTER AN INTEGER:","INTEGERS",JOptionPane.QUESTION_MESSAGE);
      array[i] = Integer.parseInt(s);

    }
    removeDuplicates(array);
   for (int i=0; i<array.length;i++) {
    JOptionPane.showMessageDialog(null,array[i],"UNIQE INTEGERS",JOptionPane.PLAIN_MESSAGE);
   }
     }
public static int[] removeDuplicates(int a []) {
   int []removedDup=new int[a.length];

  for (int i = 0; i < a.length; i++) {
    for (int j = i-1; j < a.length; j++){
      if (a[i] == a[i]) {
        removedDup[i] = j;
        break;

  }

 }
8
  • 4
    if (a[i] == a[i]) in removeDuplicates() looks like a problem Commented May 20, 2013 at 14:25
  • @TomMcIntyre provide an answer with the probably not-so-obvious problem in that line of code. Commented May 20, 2013 at 14:27
  • 1
    Unless your purpose is to learn, you could use a Set for this. docs.oracle.com/javase/tutorial/collections/interfaces/set.html Commented May 20, 2013 at 14:28
  • 2
    @Simon I would say especially if the purpose is to learn... :-) Commented May 20, 2013 at 14:28
  • @Simon by the code above, OP's in learning phase and this is an exercise (note that he/she even uses plain arrays to handle the work and not List). Commented May 20, 2013 at 14:28

4 Answers 4

1

Do I understand right that you want to get all the integers that occur only once? This can be done easily with the collections API.

public static int[] removeDuplicates(int[] a) {
    Set<Integer> unique = new TreeSet<Integer>();
    List<Integer> results = new ArrayList<Integer>();
    for (int i = 0; i < a.length; i++) {
        if (!unique.add(a[i]))
            results.add(a[i]);
    }
    int[] ret = new int[results.size()];
    for (int i = 0; i < results.size(); i++)
        ret[i] = results.get(i);
    return ret;
}
Sign up to request clarification or add additional context in comments.

Comments

0

This will do:

    public static int[] removeDuplicates(int a[]) {
    int n = a.length;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n;) {
            if (a[j] == a[i]) {
                for (int k = j; k < n-1; k++)
                    a[k] = a[k + 1];
                n--;
            } else
                j++;
        }
    }

     int[] newArray = new int[n];
     System.arraycopy(a, 0, newArray, 0, n);

     return newArray;
}

3 Comments

Won't it cause ArrayIndexOutOfBoundsException when a[k] = a[k + 1]; and k=n-1?
thanks @Shreyos Adikari for your help but i got this error!!!! how to pass it 'at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)'
Thanks soooo much man it works properly now.. @ Shreyos Adikari
0

obviously you're trying to multi loop element and compare it with others so if duplicate of that element exists you remove it and flag its index. this code you've written is buggy however I see your main problem now is that you compare element to it's very own I see if (a[i] == a[i]) to be if (a[i] == a[j]) then your code supposed to work or throw index out of bound exception

1 Comment

Without a proper explanation, this should be just a comment.
0

Scan your array for each value and compare it with each other (you should have a nested "for"), then keep a list with duplicates' indexes, instantiate an array of size a.length-listOfDuplicateIndexes.size()......fill this array with a[] components whose indexes are not int ListOfDuplicateIndexes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.