2

I ran into a seemingly simple problem that I haven't been able to figure out. Basically, my list holds a number of ints which represent different items, which works fine, but I need to be able to check if the list contains the same integer more than once and then remove them from the list.

if (myList.contains(100)) { // I want to check if the int '100' exists in myList twice
    myList.remove(new Integer(100)); // I want to remove the 2 aforementioned duplicate integers
}

Apologies if my explanation wasn't the clearest. Thanks in advance!

EDIT: To clarify, I want the list to contain duplicates but I want to be able to check if the duplicate exists X times and then remove those instances from the list.

i.e.

I might want to add the int 100 7 times, and then later check if it exists twice and then remove 2 instances of it from the list only.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Temsei
  • 41
  • 2
  • 5
  • 1
    Possible duplicate of [How do I remove repeated elements from ArrayList?](https://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – Tom May 30 '17 at 15:18
  • The dupe uses String, but it also works for Integer. – Tom May 30 '17 at 15:19

4 Answers4

2

you can create a method to accomplish the task at hand, something along the lines of this:

private static boolean removeSpecfiedNumber(int number,int numberOfTimes, List<Integer> integerList){
        if(integerList.stream().filter(x -> x == number).count() >= numberOfTimes){
             for (int i = 0; i < numberOfTimes; i++) {
                 integerList.remove((Integer)number);
             }
             return true;
        }
        return false;
}
  • The parameter number is the number you want to check its occurrences i.e. 100 in your case.
  • The parameter numberOfTimes is the number of times you want to remove that element from the list.
  • The parameter integerList is, of course, the list you want to remove the elements from.

Let's take the code below as an example:

List<Integer> myList = new ArrayList<>();
myList.add(100);
myList.add(200);
myList.add(100);
myList.add(100);
myList.add(100);
myList.add(100);
myList.add(100);
myList.add(100);
removeSpecfiedNumber(100,2,myList);
System.out.println(myList);

this will remove 100 twice from myList and should, therefore, yield the elements below:

[200, 100, 100, 100, 100, 100]

The method return type can be void if you want, However, the return type of boolean can come in handy at times hence I've used that approach.

Also, you need not use the static modifier if you're dealing with objects, therefore you can remove it.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Thanks for your answer! I don't want to remove all of them though. I need the list to contain the same ints many times, I just need on different occasions check if '100' exists twice or more and then remove those values only. – Temsei May 30 '17 at 15:37
  • Thanks so much! Works just as I hoped it to. Would you mind explaining the process behind this line? ` if(integerList.stream().filter(x -> x == number).count() >= 2){` – Temsei May 30 '17 at 16:13
  • @Temsei that line ensures that the element you want to remove ( i.e `100`) has occurred at least the `numberOfTimes` you want to remove it from the list. for example, if `100` only appeared once within the list and you wanted to remove it `2` times, it won't remove it. This is essentially to guard off potential errors. – Ousmane D. May 30 '17 at 16:16
  • To put it simply, you _cannot_ remove `x` number of elements from the list when there is not that many of them. – Ousmane D. May 30 '17 at 16:23
1

1) myList.remove(new Integer(100)); will remove only the first occurrence that is equals to 100.
You should loop on remove() while the list contains still an object with the same value.

2) To know if the list contains more than once the object, you could use indexOf() and lastIndexOf() .
If these are distinct, it means that you have more than one element.
So according to your requirement, you can remove all of them with the method described in the point 1.

  Integer valueToCheck = 100;
  if ( myList.indexOf(valueToCheck) != myList.lastIndexOf(valueToCheck) ) {   
       while (myList.contains(valueToCheck)){
          myList.remove(valueToCheck);  
       }
   }
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

You can use a Set which does not allowed duplicate keys e.g.

Set<Ineger> foo = new HashSet<>(myList);

And you can create a new List from that or use it as it is.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

Starting java 8 you can use java stream api in such way:

List<Integer> i  = new ArrayList<Integer>();
    i.add(1);
    i.add(2);
    i.add(3);
    i.add(1);

List<Integer> collect = i.stream().distinct().collect(Collectors.toList());

NOTE: new list will be created.

Oleksandr Papchenko
  • 2,071
  • 21
  • 30