1public static void main(String[] args) {
2
3 String result = removeDup("AAABBBCCC");
4 System.out.println(result); // ABC
5
6public static String removeDup( String str) {
7 String result = "";
8 for (int i = 0; i < str.length(); i++)
9 if (!result.contains("" + str.charAt(i)))
10 result += "" + str.charAt(i);
11 return result;
12 }
13}
1import java.util.*;
2
3public class RemoveDuplicatesFromArrayList {
4
5 public static void main(String[] args) {
6 List<Integer> numbers = Arrays.asList(1,2,2,2,3,5);
7
8 System.out.println(numbers);
9
10 Set<Integer> hashSet = new LinkedHashSet(numbers);
11 ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);
12
13 System.out.println(removedDuplicates);
14 }
15}
16
1String fullString = "lol lol";
2String[] words = fullString.split("\\W+");
3StringBuilder stringBuilder = new StringBuilder();
4Set<String> wordsHashSet = new HashSet<>();
5
6for (String word : words) {
7 if (wordsHashSet.contains(word.toLowerCase())) continue;
8 wordsHashSet.add(word.toLowerCase());
9 stringBuilder.append(word).append(" ");
10}
11String nonDuplicateString = stringBuilder.toString().trim();
1public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
2 Set<T> set = new LinkedHashSet<>(list);
3 return new ArrayList<T>(set);
4}
1List<Integer>numbers = Arrays.asList(1,2,2,2,3,5); // [1, 2, 3, 5]
2System.out.println(numbers);
3
4Set<Integer> hashSet = new LinkedHashSet(numbers);
5ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);
6System.out.println(removedDuplicates); // [1, 2, 3, 5]