Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:15:26+00:00 2026-06-01T04:15:26+00:00

I am trying to write a very simple method to remove duplicates in a

  • 0

I am trying to write a very simple method to remove duplicates in a LinkedList:

I try to do this without using additional buffer, so I maintain two iterators on the linked list, one does the normal iteration, and another iterates through all prior nodes to check for dupes (as indicated in CareerCup); however, the compiler tells me there is a CME even though I am calling itr1.remove():

public static void RemoveWithoutBuffer(LinkedList l) {
    ListIterator itr1 = l.listIterator();   
    int count1 = 0;
    int count2 = 0;
    while (itr1.hasNext()) {
        Object next = itr1.next();
        count1++;
        count2 = 0;
        ListIterator itr2 = l.listIterator();
        while (itr2.hasNext()) {
            count2++;
            if (count2 == count1)
                break;
            if (itr2.next() == next){
                itr1.remove();
            }
        }

    }
}

Another simpler solution of this problem with the aid of hashset is easy as follows, and no exception reported:

    public static void Remove(LinkedList l) {
    HashSet set = new HashSet();
    ListIterator itr = l.listIterator();
    while (itr.hasNext()) {
        Object next = itr.next();
        if (set.contains(next))
            itr.remove();
        else
            set.add(next);
    }
}

Is it because when I am iterating through itr2 I cannot modify on itr1? Is there a way to fix this? Thank you guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:15:28+00:00Added an answer on June 1, 2026 at 4:15 am

    In the first case yes – you’re altering the collection’s contents via iterator2, while iterator1 is not aware about the changes. In the second case HashSet/HashMap don’t allow removing elements while iterating through them.

    You can add removed elements to another collection, and removeAll them after an iteration. E.g.

        List toRemove = new ArrayList();
        for (Object next : collection) {
            if (someCondition) toRemove.add(next);
        }
        collection.removeAll(toRemove);
    

    I hope it helps.

    P.S. more details on how to remove elements from list, concerning algorithm complexity you can read here Removing ArrayList object issue

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a VERY simple UI in Python using Tkinter. I
I am trying to write a very simple RSS channel which would display the
I'm trying to write a very simple image processing program for fun and practice.
I'm trying to write a very simple program, I want to print out the
For fun I'm trying to write a very simple server in C. When I
I'm trying to write a very simple plugin that on anchor mouseover increases the
I'm trying to write a very simple email script in python. It's basically a
I am trying to write a very simple app that displays the name of
I'm trying to develop a very simple Java web application using JSP and Servlets.
I am trying to write a very simple application that allows me to enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.