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

  • Home
  • SEARCH
  • 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 8856113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:17:13+00:00 2026-06-14T14:17:13+00:00

So, a long story short, I have a Java homework assignment that requires a

  • 0

So, a long story short, I have a Java homework assignment that requires a long ArrayList of Strings to be manipulated in various ways (we’re doing things like showing combinations of words, adding and removing from the ArrayList, nothing too special). I noticed that a few of the provided ArrayLists have duplicate entries (and the duplicates aren’t necessary for this assignment), so I got the okay from my teacher to sanitize the data by removing duplicate entries.
Here’s what I came up with:

private static ArrayList<String> KillDups(ArrayList<String> ListOfStrings) {  

    for (int i = 0 ; i < ListOfStrings.size(); i++) {
        for (int j = i + 1; j < ListOfStrings.size(); j++) {
            //don't start on the same word or you'll eliminate it.
            if ( ListOfStrings.get(i).toString().equalsIgnoreCase( ListOfStrings.get(j).toString() )  ) {
                ListOfStrings.remove(j);//if they are the same, DITCH ONE.
                j = j -1; //removing the word basically changes the index, so swing down one.
            }                                
        }
    }
    return ListOfStrings;
}

This is fine for my assignment, but I doubt it would be very useful in the real world. Is there a way to do this that would ignore white space and special characters during the comparison? Is there a cleaner way in general to handle this (maybe without the nested For Loops)? Is there another question I should be asking that I don’t know to ask?

  • 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-14T14:17:14+00:00Added an answer on June 14, 2026 at 2:17 pm

    Yes. And it can be done in just 1 (elegant) line:

    List<String> noDups = new ArrayList<String>(new LinkedHashSet<String>(list));
    

    The intermediate Set ensures no duplicates. The LinkedHashSet implementation of Set was chosen to preserve the order of the list.

    Also, on a style note:

    • name your methods and parameters with names starting with a lowercase letter
    • always refer to the abstract (ie List) rather than the concrete (ie ArrayList) when specifying method signatures

    Your whole method is then:

    private static List<String> killDups(List<String> list) {
        return new ArrayList<String>(new LinkedHashSet<String>(list));
    }
    

    For extra brownie points make the method generic, so it works with any type of List:

    private static <T> List<T> killDups(List<T> list) {
        return new ArrayList<T>(new LinkedHashSet<T>(list));
    }
    

    If you wanted to ignore certain characters, I’d create a class for that and have a list of those. Both the hashCode() and the equals() methods are relied upon by HashSets to remove dups:

    public class MungedString {
        // simplified code
        String s;
    
        public boolean equals(Object o) {
            // implement how you want to compare them here
        }
    
        public int hashCode() {
            // keep this consistent with equals()
        }
    }
    

    then

    List<MungedString> list;
    List<MungedString> noDupList = killDups(list);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long story short, I have a Java process that reads and writes data to/from
So long story short, i have some forms (2) of them that, need different
To cut a long story short I have a section of my application that
To cut a long story short I have a C# function that performs a
Ok, long story short I have a Windows service that handles Win32_VolumeChangeEvent and logs
I have a java application running on windows machines. Long story short, we have
Long story short: I have some controller logic that requests a value from the
Long story short -- we have 2 webservers that run a 3rd party asp.net
Long story short, I have a substantial Python application that, among other things, does
Long story short, I have an ASP.NET application I'm trying to debug and at

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.