-1

I build removing duplicates, however I'm thinking how to use a method that removes the duplicate elements from an array list of integers using the following header:

public static void removeDuplicate(ArrayList<Integer> list)

write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space.

import java.util.ArrayList;

import java.util.Scanner;

public class RemoveDuplicates {
    public static void main(String[] args){
     ArrayList<Integer>list = new ArrayList<Integer>();

     Scanner input = new Scanner (System.in);
     System.out.print("Enter integers (input ends with 0): ");
     int value;

     do{
         value = input.nextInt();
         if(!list.contains(value)&& value !=0)
             list.add(value);
     }while (value !=0);

     input.close();
     for (int i = 0; i < list. size(); i++)
          System.out.print(list.get(i) + " ");
     }
}

It's my code,please modifying please, how to use method and test .

GameDroids
  • 5,584
  • 6
  • 40
  • 59
Young
  • 1
  • 1
  • 3
  • How can I delete? Sorry, this question has answers and cannot be deleted; flag it for moderator attention instead. This message shows. – Young Nov 12 '14 at 22:58
  • you could also just leave it here. There is absolutely nothing wrong with this question. And maybe someone else might profit from this thread. Additionally you can accept one of the answers by clicking on the transparent "hook" left of it. This way other readers have it easier to find the answer, that helped you the most and others won't try to respond since the "matter is closed" – GameDroids Nov 12 '14 at 23:04

2 Answers2

1

If I understand correctly, you should implement a method with this header

public static void removeDuplicate(ArrayList<Integer> list)

judging by its name, I'd say that method should remove the duplicates from the list and not (as you are doing it right now) the do-while-loop during input.

So first remove the check in your loop (if(!list.contains(value)&& value !=0)) and just add every number the user types to the list.

Then you can make a call to the method removeDuplicate(list);. If you want you can add this call in your loop and it will be executed after every input or you execute it just once when the input is closed.

Now implementing the method:

public static void removeDuplicate(ArrayList<Integer> list) {  // this is the header you need to use

The problem here is, the method knows the list but not the element that is a possible duplicate. So you have to look for it

    for (int i = 0; i < list.size(); i++) {  // iterate through every element in the list 
        Integer current = list.get(i);       // for convenience, save the current list item in a variable

So, you check every integer in the list – one by one.. but if you want to know if the integer exists a second time, you have to search the tail of the list. Meaning you have to check the sublist after i.

        List sublist = list.subList(i + 1, list.size());  // the sublist with all elements of the list from i+1 to the end

your list.contains(value) line is correct, you can use it here as well. Only now you call it on the sublist

       if(sublist.contains(current)){   // checks if the number is in the sublist
             sublist.remove(current);   // removes the number from the sublist
       }

This however would only remove the first duplicate. Alternatively, you can remove every item in the list that equals the current integer:

        while (sublist.contains(current)) {
            sublist.remove(current);
        }

And that's it. Your method is finished.

    }
}

It is finished because you are actually working on the one and only list in your program. Even when you remove an integer from your sublist, it is actually removed from the sublist and the real list (the sublist is just a reference, and not an actual list on its own)

EDIT

for your convenience here the complete code with both methods. If you compare the code to yours, you'll see that there is not much different:

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<Integer>();

    Scanner input = new Scanner(System.in);
    System.out.print("Enter integers (input ends with 0): ");
    int value;

    do {
        value = input.nextInt();
        if (value != 0) {     // this changed: add every number except 0
            list.add(value);
        }
    } while (value != 0);

    input.close();

    removeDuplicate(list);    // here you make the call for the new method

    for (int i = 0; i < list.size(); i++) {
        System.out.print(list.get(i) + " ");
    }
}

// and this is the new method
public static void removeDuplicate(ArrayList<Integer> list) {
    for (int i = 0; i < list.size(); i++) {
        Integer current = list.get(i);
        List sublist = list.subList(i + 1, list.size());
        while (sublist.contains(current)) {
            sublist.remove(current);
        }
    }
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • 1
    :) just remove my comments and copy the code lines one by one in your editor, and you have your complete method `removeDuplicate`. Put this method in the same file you are working on right now, just put it *under* the `public static void main(String[] args){ ...} `method – GameDroids Nov 06 '14 at 21:55
  • the only thing you have to change is: **remove** `if(!list.contains(value)&& value !=0)` and **add** `removeDuplicate(list);` after `input.close();` – GameDroids Nov 06 '14 at 21:57
  • I guess it is just a warning when getting the `sublist`. The method `list.subList(start,end)` can only return a list of type `List`. This type however is usually not instantiated on its own because it is just an `Interface`. You would use `ArrayList` or similar for example. However here you don't need to worry about this, because your sublist comes from an `ArrayList`, which is fully implemented and you have everything you need. Maybe your IDE program is just warning you, but the compiler should recognize that `sublist` is an `ArrayList` too. – GameDroids Nov 06 '14 at 22:29
  • You don't need to delete any posts. If my answer helped you, just check the "answered" button. Otherwise there is no messaging system here in SO. Just post a new question next time :) – GameDroids Nov 06 '14 at 22:52
0

If you don't want duplicate, use a collection that implements the Set interface (http://docs.oracle.com/javase/7/docs/api/java/util/Set.html) instead of an array list.

user1283559
  • 39
  • 1
  • 6