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 8454477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:02:36+00:00 2026-06-10T12:02:36+00:00

Thanks Marko. I rewrite the code. try to make it simple. this time it

  • 0

Thanks Marko. I rewrite the code. try to make it simple. this time it can really compile. but it can only delete duplicate items sit next to each other. for example, if i put in 1 2 3 3 4 4 5 1 — the output is 1 2 3 4 5 1. it can’t pick up the duplicate at the end.
(BTW: new to this website, if make any display mess my apologies)

here’s the new code:

import java.util.*;

public class SetListDemo{
public static void main(String[] args){
    SetListType newList = new SetListType();
    Scanner keyboard = new Scanner(System.in);

    System.out.println( "Enter a series of items: ");
        String input = keyboard.nextLine();

    String[] original = input.split(" ");
    for (String s : original)
    newList.insert(s);

    List<String> finalList = new ArrayList(Arrays.asList(original)) ;

    Iterator<String> setIterator = finalList.iterator();  

    String position = null;

    while(setIterator.hasNext()){
        String secondItem = setIterator.next();

        if(secondItem.equals(position)){
            setIterator.remove();
        }   

        position = secondItem;
    }

    System.out.println("\nHere is the set list:");
    displayList(finalList);
    System.out.println("\n");
}

public static void displayList(List list){
    for(int index = 0; index <list.size(); index++)
    System.out.print(list.get(index) + ", ");
}

}
  • 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-10T12:02:37+00:00Added an answer on June 10, 2026 at 12:02 pm

    To answer the question “delete duplicates in java arraylist”:

    Just put all elements into a Set and you’re done.

    -or-

    Iterate your original list and add the elements to a List, but before adding them, check with List#contains() if the element is already there.

    EDIT: Try this:

    String[] original = input.split(" ");
    List<String> finalList = new ArrayList<String>();
    
    for (String s : original) {
        if (!finalList.contains(s)) {
            finalList.add(s);
        }
    }
    
    System.out.println("\nHere is the set list:");
    displayList(finalList);
    System.out.println("\n");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some legacy code that I really don't have time to rewrite. Is
Thanks for stopping by. this static analyser warning is annoying me here is my
Thanks for reading this. I have no idea why this is throwing a NullReferenceException
Thanks in advance to anyone who can think of a more efficient or a
I suspect this has to do with endianness but I'm not sure how to
This sounds like a stupid question while I'm asking it, but I'm new to
I'm having a hell of a time getting this to cooperate. Basically I'm trying
I've just created a simple, continuous bounce effect in jQuery but I feel the
Anyone got any neat solutions to prevent a Silverlight ChildWindow being moved? thanks, Mark
Thanks for the help on here with my query on here the other day

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.