-1

Let's say I've got a list of Strings (simplification)

fullList = {a,b,c,d,a,d,c,b}

and I want to find couples like

couplesList = {{a,a},{b,b}, ...}

The way I'm approaching this problem at the moment is

  1. Get first element
  2. Use guava predicate to find proper object
  3. What now?

I'm ending up having 2 objects {a,a} but I can't remove them from fullList because I'm not using an "iterator" style of iterating (because I'm using Guava predicate it wouldn't work anyway - since I don't have iterator pointer to element that was find by Itarables.find(...) function).

I want to do it in "efficient" way as well, so I want to avoid multiple nested loops etc.

Any ideas how to approach this problem more correctly/efficient way ? I'm bit stuck.

4

1 回答 1

6

I would create a frequency count for each of the elements. In Guava terms this is a MultiSet. From that you can create a collection of pairs, and another collection of the singles. This can be done with one pass of the original list and one pass of the frequency count map. i.e. O(n)

于 2013-10-06T17:51:45.200 回答