40122/remove-duplicates-from-an-arraylist-in-java
I was taking a test and was stuck with this program can someone help me find a solution?
I want to remove all the duplicate elements in an arraylist.
eg:
1 2 3 1 2 3 4 5 4 5 6 7 8 7 6 7 8 Output: 1 2 3 4 5 6 7 8
Here is the function for doing this.
// Function to remove duplicates from an ArrayList public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) { // Create a new ArrayList ArrayList<T> newList = new ArrayList<T>(); // Traverse through the first list for (T element : list) { // If this element is not present in newList then add it if (!newList.contains(element)) { newList.add(element); } } // return the new list return newList; }
List<String> al = new ArrayList<>(); // add elements ...READ MORE
You can use ArrayUtils class remove method ...READ MORE
In Java 8 or earlier: List<String> string = ...READ MORE
import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE
Yes; the Java Language Specification writes: In the Java ...READ MORE
We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE
The thing you are worried about is ...READ MORE
You can use this method: String[] strs = ...READ MORE
The TLE (Time limit exceed) problem occurs ...READ MORE
The different ways of comparing string in ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.