0

How do I reset a for-loop while looping through a list? For example, I want the for-loop to reset i.e. (i=0 and j=1) when there are duplicates in a list .

In this piece of code, I want the duplicates removed and the for-loop reset when subsequent entries in the list are equal. For instance, we have

list1 = [east, west, west, east]

I want the resultant list1 to be equal to an empty list.

This is because, When both "west" entries are eliminated, this results in the list updating to [east,east]. Since this is also a duplicate, the result must hence be an empty list [].

j=1;
for (int i=0;i<(list1.size()-1);i++){
    if((list1.get(i)==list1.get(j))){
        list1.remove(i);
        list1.remove(i);
        i=0;
        j=1;
    }else{
        j++;
    }
}
tsizzle9
  • 21
  • 1
  • 3
  • you can use collection rather than using writing login to remove duplicate data. – mohan rathour Dec 16 '16 at 11:20
  • Could you please elaborate on the part you have a problem with? Your question is about 'how to reset a loop' but you code already has the needed lines to reset the loop. – Viktor Seifert Dec 16 '16 at 11:29
  • For this you should be using the lists iterator. – ASA Dec 16 '16 at 11:32
  • You should use Set instead of a normal list =D, in set duplicate entries are not allowed. – Muhammad Dec 16 '16 at 11:32
  • Do you want to remove only consecutive dupes? e.g. `east, west, east, west` would not resolve into an empty list. What about `east, west, west, west, east`, should it become `east, west, east`? – SubOptimal Dec 16 '16 at 11:47

5 Answers5

1

If you want the duplicates removed, why don't you use a Set ?

String[] list1 = {"east", "west", "west", "east"};
List<String> list = new ArrayList<>(Arrays.asList(list1));
Set<Object> alreadyPresent = new HashSet<>();

Iterator<String> iterator = list.iterator();
for (String element : new ArrayList<String>(list)) {
    if (!alreadyPresent.add(element)) {
        while(list.remove(element));
    }
}

Edit (Much better) :

String[] list1 = {"a","b","b","a","d","e","f"};
List<String> list = new ArrayList<>(Arrays.asList(list1));

for (String element : new ArrayList<String>(list)) {
    if(Collections.frequency(list, element) > 1) {
        while(list.remove(element));
    }
}
r3n0j
  • 71
  • 7
  • I want both entries to be removed if they present subsequently in the list. For example if list contains: "a","b","b","a","d","e","f" It should should be updated to: "d","e","f" Because when "b","b" are BOTH removed, you have "a","a","d","e","f" hence both "a" entries also need to be removed giving: "d","e","f" – tsizzle9 Dec 16 '16 at 12:02
  • @tsizzle9 : I updated my answer. – r3n0j Dec 16 '16 at 14:01
1

You can loop through the ArrayList reversely:

ArrayList<String> list1 = new ArrayList<String>(Arrays.asList(new String[]{"east", "west", "west", "east", "foo"}));
for (int i = (list1.size() - 2);i >= 0;i--){
    for(int j = (list1.size() - 1);j > i;j--) {
        if((list1.get(i).equals(list1.get(j)))) {
            list1.remove(i);
            list1.remove(i);
        }
    }
}
System.out.println(list1);
salix
  • 846
  • 6
  • 13
0
    List<String> list1 = new ArrayList<String>();
    list1.add("east");
    list1.add("east");
    list1.add("west");
    list1.add("test");
    list1.add("west");
    int j=1;
    for (int i=0;i<list1.size();i++){
        //here you can say if you want exactly two or more
        if(Collections.frequency(list1, list1.get(i)) > 1) {
            list1.removeAll(Collections.singleton(list1.get(i)));
            i=0;
        }

    }
    System.out.println(list1);
ddarellis
  • 3,912
  • 3
  • 25
  • 53
0

Try modularizing your code a little bit more!

// The function you are trying to impliment
void removePairs(List<Object> list) {
    while (removePair(list)) {}
}

Let's use a helper method to make our life easier

// Return true if successfully removed a pair
boolean removePair (List<Object> list) {
    for(i = 0; i < list.size() - 1; i++) {
        // Get the next objects
        Object firstObject = list.get(i);
        Object secondObject = list.get(i + 1);

        if (firstObject.equals(secondObject)) {
                list.remove(i);
                list.remove(i + 1);
                return true;
        }
    }

    return false;
}

One other note, j = 1 should not be where it is. I am referring to variable scope. In your original code, you won't (hopefully) care about j after the for loop completes. But it is still hanging around, waiting to cause bugs when it gets used for something that it shouldn't!

alexdriedger
  • 2,884
  • 2
  • 23
  • 30
0

To state the problem: if in the sequence to duplicate values appear [..., a, a, ...] you want to remove them, and recurse.

Most readable would be to do away with j or do int j = i - 1;.

    List<String> list = new ArrayList<>();
    Collections.addAll(list, "east", "west", "west", "east");
    for (int i =  1; i < list.size(); ++i) {
        String value = list.get(i);
        int priorI = i - 1;
        if (value.equals(list.get(priorI))) {
            list.remove(priorI);
            list.remove(priorI);
            // next i will be priorI but at least 1
            i = Math.max(0, priorI - 1); // With ++i will be > 0
        }
    }
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138