-1

For example:

ArrayList<Integer> a = new ArrayList<Integer>();

while(a.size() < 50){
        a.add(1);
        a.add(2);
        a.add(9);
    }
    System.out.println(a);

I would like to remove all the excess 1's, 2's, and 9's, but keep one of each.

Not cleared: [1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9, 1, 2, 9]

Cleared: [1, 2, 9]

Ideally I would like this to work for any x value that occurs any n number of times in the ArrayList.

My partial solution just returns whether or not a value occurs more than once (true if it occurs more than once, false if it doesn't)

public static boolean occ(ArrayList<Integer> l, int s) {

    int n = 0;
    boolean t = false;

    for (int p : l) {
        if (p == s) {
            n++;
        }
    }

    if (n > 1) {
        t = true;
    }

    return t;
}

Method implementation:

occ(ArrayList, Selected Int Value to be Checked);

If I change to a HashSet or Set, would I be able to implement the ArrayList methods clear() and size()?

  • 3
    Any reason you don't want to use a `HashSet` or `LinkedHashSet`?? – Jon Skeet Aug 23 '16 at 12:22
  • No, but how would those help? –  Aug 23 '16 at 12:23
  • @HanKurschev: Um, well they'd keep a single example of each value, that being what sets do... – Jon Skeet Aug 23 '16 at 12:25
  • This is not a duplicate because it is keeping one of the original values. –  Aug 23 '16 at 12:25
  • Yes, I looked at them, I will try them. –  Aug 23 '16 at 12:26
  • @JonSkeet: But would I still be able to implement ArrayList methods such as `clear()` and `size()`? –  Aug 23 '16 at 12:27
  • How about you look at the documentation for the `Set` interface? (Before asking a question on SO, either as a post or in comments, you should ask yourself whether you can reasonably find out for yourself with a bit of research.) – Jon Skeet Aug 23 '16 at 12:41

2 Answers2

1
public static List<Integer> withoutDuplicates(List<Integer> withDuplicates) {
    List<Integer> retVal = new ArrayList<>();
    Set<Integer> alreadyParsed = new HashSet<>();
    for (Integer val : withDuplicates) {
        if (!alreadyParsed.contains(val)) {
             retVal.add(val);
             alreadyParsed.add(val);
        }
    }
    return retVal;
}

or the Java 8 version

withDuplicates.stream().distinct().collect(Collectors.toList());
Piotr Wilkin
  • 3,446
  • 10
  • 18
  • Why not just `new ArrayList<>(new LinkedHashSet<>(withDuplicates))` for the first part? – Jon Skeet Aug 23 '16 at 12:26
  • Added the streams version. As for the first one - because it might not preserve the original order. EDIT: sorry, `LinkedHashSet` should preserve it. It's OK then. – Piotr Wilkin Aug 23 '16 at 12:27
  • To much code to do simple convertion – Sergei Rybalkin Aug 23 '16 at 12:28
  • Also, it's better `withDuplicates.stream().collect(Collectors.toSet())` – Sergei Rybalkin Aug 23 '16 at 12:34
  • No, it's not, because it won't preserve ordering. From the documentation: *For ordered streams, the selection of distinct elements is stable (...)* – Piotr Wilkin Aug 23 '16 at 12:35
  • Who cares about ordering in set? – Sergei Rybalkin Aug 23 '16 at 12:37
  • I don't know, but the original question wasn't about a set, it was about a list with removed duplicates. Obtaining a list with duplicates removed is a different problem than obtaining a set of elements. – Piotr Wilkin Aug 23 '16 at 12:39
  • To give you a real-life example: suppose you have a list of customer requests for contact and you want to get list of customers to contact in the order that the requests are made. Obviously, in this case you want a list with removed duplicates and not a set. – Piotr Wilkin Aug 23 '16 at 12:41
1
Set<Integer> set = new HashSet<Integer>(listWithDup);

From doc:

Set - a collection that contains no duplicate elements

Sergei Rybalkin
  • 3,337
  • 1
  • 14
  • 27