2

I have a List of integer's with duplicate values in it. What I need to do is find the duplicate integers, add their value and then add the result to the list by removing the duplicates found. Here is what I am doing:

List<Integer> list1 = new ArrayList<Integer>();
    list1.add(2);
    list1.add(5);
    list1.add(3);
    list1.add(5);
    list1.add(4);

    List<Integer> list2 = new ArrayList<Integer>();
    Iterator<Integer> it = list1.iterator();
    while (it.hasNext()) {
        Integer int1 = it.next();
        if (list2.isEmpty()) {
            list2.add(int1);
            it.remove();
        } else {
            ListIterator<Integer> it2 = list2.listIterator();
            while (it2.hasNext()) {
                Integer int2 = it2.next(); 
                if (int2 != int1) {
                    it2.add(int1);
                    it.remove();// I get exception here

                } else {                        
                    it2.remove();
                    it.remove();
                    Integer newint = int1 + int2;
                    it2.add(newint);
                }                   
            }
        }
    }       
    for(Integer in : list2){
        System.out.println(in);
    }

Output should look like
2
10
3
4

Thanks for your time.

1
  • Just as a side comment, it's recommendable to refer to interfaces instead of concrete implementations (i.e. write List<Integer> list1 = new ArrayList<Integer>();) Commented Oct 4, 2011 at 22:32

5 Answers 5

3

As the other posters have said, you can't remove while iterating. Even though there are 'tricks', messing with a collection while iterating is a surefire way to get weird runtime bugs.

Anyway, you are working way too hard on the problem.

Here's a quick and dirty solution with a fraction of the code:

private List<Integer> sumAndUniqDuplicates(List<Integer> list) {
    LinkedHashMap<Integer, Integer> lookup = new LinkedHashMap<Integer, Integer>();
    for (Integer value : list) {
        Integer prevValue = lookup.get(value);
        prevValue = (prevValue == null) ? 0 : prevValue;
        lookup.put(value, prevValue + value);
    }
    return new ArrayList<Integer>(lookup.values());
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you are allowed to use a Map you could do something simple like this (incoming pseudocode):

create empty Map m
for each Integer x in list1 do
    if m does not contain key x 
        m.put(x, x)
    else
        m.put(x, m.get(x) + x)
    endif
done

Your result are the values of m (which is a Collection).

Edit: You said you have LatLng instead of Integers - I don't know LatLng but after a quick google I'd take a shot at the following, assuming that you want to "add" up your LatLng points:

create empty Map<LatLng, LatLng> m
for each LatLng x in list1 do
    if not m.containsKey(x) 
        m.put(x, x)
    else
        m.put(x, LatLng.newInstance(m.get(x).getLatitude() + x.getLatitude(),
                                    m.get(x).getLongitude() + x.getLongitude()))
    endif
done

The only problem I can see here is that this m.containsKey(x) depends on the correct implementation of equals, which I'm not sure after reading this

2 Comments

The thing is I cannot use Map, I am actually working with google maps and I have LatLng points instead of Integer, so when I as to get some thing on map with LatLng point it returns null :(
This is what I tried and it works after overwriting equals method. Thanks for the answer.
2

It is because you remove the same element twice. First time in if(list2.isEmpty()) (because list2 is empty in the beginning and immediately after that in the else body.

1 Comment

Any suggestions on how to overcome this issue ? Even if I dont remove first time, I mean in list2.isEmpty block, it still gives me the error.
1

From the documentation for the remove method:

Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next.

Comments

1

You cannot remove the current element twice. You need to rethink your logic.

1 Comment

Any suggestions on how to achieve it?

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.