Skip to content
Fix Code Error

How do I remove repeated elements from ArrayList?

March 13, 2021 by Code Error
Posted By: Anonymous

I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?

Solution

If you don’t want duplicates in a Collection, you should consider why you’re using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);

Of course, this destroys the ordering of the elements in the ArrayList.

Answered By: jonathan-stafford

Related Articles

  • HashSet vs. List performance
  • Define: What is a HashSet?
  • bootstrap-datepicker in dd/mm/yyyy - selected date…
  • Java Compare Two Lists
  • Hashset vs Treeset
  • How to filter a RecyclerView with a SearchView
  • How to turn off ALL conventions in Entity Framework Core 5
  • Why is there no SortedList in Java?
  • Using Event Aggregator to load a view with different…
  • WAVE file unexpected behaviour
  • Java Array, Finding Duplicates
  • Remove duplicates from a List in C#
  • foobar.withgoogle.com task Please Pass the Coded…
  • Get value (String) of ArrayList(); in Java
  • Error message "Forbidden You don't have permission…
  • backbone.js collection bind this confusion
  • How to maintain a Unique List in Java?
  • Backbone "reset" collection trigger not firing
  • What does the CSS rule "clear: both" do?
  • What are the undocumented features and limitations…
  • Memcached vs. Redis?
  • When to use LinkedList over ArrayList in Java?
  • Fastest way to iterate over all the chars in a String
  • Thread safe factory
  • HashSet vs LinkedHashSet
  • htaccess "order" Deny, Allow, Deny
  • Geolocation denied for HTML embedded site - anchor…
  • Why doesn't the height of a container element…
  • Efficiency of Java "Double Brace Initialization"?
  • Logging best practices
  • C# Set collection?
  • What is a "cache-friendly" code?
  • Getting the closest string match
  • Backbone: Multiple views subscribing to a single…
  • No 'Access-Control-Allow-Origin' header is present…
  • SQL query return data from multiple tables
  • Ukkonen's suffix tree algorithm in plain English
  • Save ArrayList to SharedPreferences
  • Does adding a duplicate value to a HashSet/HashMap…
  • VueJs single file component not reading data/props/methods
  • Backbone Collection.fetch gives me Uncaught…
  • Java: Retrieving an element from a HashSet
  • Java: Detect duplicates in ArrayList?
  • How to generate a random string of a fixed length in Go?
  • How do I change db schema to dbo
  • How to add elements of a Java8 stream into an existing List
  • How to paste yanked text into the Vim command line
  • Knight's tour Problem - storing the valid moves then…
  • What is an optional value in Swift?
  • How do I install Java on Mac OSX allowing version switching?
  • Most efficient way to see if an ArrayList contains…
  • What is your most productive shortcut with Vim?
  • How to use setOnAction with checkBox?
  • XMLHttpRequest cannot load ✘✘✘ No…
  • Remove elements from collection while iterating
  • Polling a Collection with Backbone.js
  • Trigger Function in Collection View after Backbone Sync
  • How to declare an ArrayList with values?
  • Backbone Marionette Nested Composite View
  • Removing Duplicate Values from ArrayList
  • Sorting an ArrayList of objects using a custom sorting order
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Angular/Typescript @Output with Union return types
  • Debugging Backbone.js: rendering after collection fetch()
  • get data from server Backbone.js application
  • Remove Elements from a HashSet while Iterating
  • How to filter a Boolean field in a json join
  • Difference between HashSet and HashMap?
  • Using UPDATE in stored procedure with optional parameters
  • Add ArrayList to another ArrayList in java
  • Can a local variable's memory be accessed outside its scope?
  • How to Connect a Django Model with ManyToMany Relationship?
  • What is a NullReferenceException, and how do I fix it?
  • Access-Control-Allow-Origin is not allowed by…
  • What exactly is std::atomic?
  • How to convert an XML file to nice pandas dataframe?
  • Build Polymer App with multiple elements using one…
  • How to add elements of a string array to a string…
  • Backbone: How to update a collection view when…
  • Animate an element's width from 0 to 100%, with it…
  • How does PHP 'foreach' actually work?
  • How Spring Security Filter Chain works
  • java.lang.IllegalStateException in iterator.remove()
  • How to make Java Set?
  • What is the difference between ArrayList.clear() and…
  • What does a "Cannot find symbol" or "Cannot resolve…
  • data.table vs dplyr: can one do something well the…
  • Static Vs. Dynamic Binding in Java
  • How to deserialize Arraylist items into concrete…
  • Identifying and solving…
  • How to tell Entity Framework to not include a nested object?
  • Java: Check if enum contains a given string?
  • Vaadin: Tabs are displayed in a really bizarre way -…
  • Add multiple items to already initialized arraylist in java
  • Backbone.js Todo example with nested todos
  • Intersection and union of ArrayLists in Java
  • Backbone.js navigation working once but not again
  • HashMap(key: String, value: ArrayList) returns an…
  • Sorting Backbone Collections
  • How to enable local network users to access my WAMP sites?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to revert uncommitted changes including files and folders?

Next Post:

How to check certificate name and alias in keystore files?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error