Answers for "remove duplicates from string java"

3

java remove duplicates

import java.util.*;

public class RemoveDuplicatesFromArrayList {

    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1,2,2,2,3,5);

        System.out.println(numbers);

        Set<Integer> hashSet = new LinkedHashSet(numbers);
        ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);

        System.out.println(removedDuplicates);
    }
}
Posted by: Guest on June-12-2020
1

String remove duplicate method in java

public static void main(String[] args) {

        String result = removeDup("AAABBBCCC");
        System.out.println(result); // ABC

public static  String  removeDup( String  str) {
        String result = "";
        for (int i = 0; i < str.length(); i++)
            if (!result.contains("" + str.charAt(i)))
                result += "" + str.charAt(i);
        return result;
    }
}
Posted by: Guest on June-04-2021
2

java program to remove duplicate words in a string

String fullString = "lol lol";
String[] words = fullString.split("\\W+");
StringBuilder stringBuilder = new StringBuilder();
Set<String> wordsHashSet = new HashSet<>();

for (String word : words) {
    if (wordsHashSet.contains(word.toLowerCase())) continue;
    wordsHashSet.add(word.toLowerCase());
    stringBuilder.append(word).append(" ");
}
String nonDuplicateString = stringBuilder.toString().trim();
Posted by: Guest on May-22-2021
3

how to remove duplicates from an array java

// Must sort arrays first --> Arrays.sort(arrayName)
public class RemoveDuplicateInArrayExample{  
public static int removeDuplicateElements(int arr[], int n){  
        if (n==0 || n==1){  
            return n;  
        }  
        int[] temp = new int[n];  
        int j = 0;  
        for (int i=0; i<n-1; i++){  
            if (arr[i] != arr[i+1]){  
                temp[j++] = arr[i];  
            }  
         }  
        temp[j++] = arr[n-1];     
        // Changing original array  
        for (int i=0; i<j; i++){  
            arr[i] = temp[i];  
        }  
        return j;  
    }  
       
    public static void main (String[] args) {  
        int arr[] = {10,20,20,30,30,40,50,50};  
        int length = arr.length;  
        length = removeDuplicateElements(arr, length);  
        //printing array elements  
        for (int i=0; i<length; i++)  
           System.out.print(arr[i]+" ");  
    }  
} 
Posted by: Guest on April-17-2020
1

java remove duplicates

public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
  Set<T> set = new LinkedHashSet<>(list);
  return new ArrayList<T>(set);
}
Posted by: Guest on June-16-2020
0

String remove duplicate in java

String str1 = "ABCDABCD";
String result1 = "";

for (int a = 0; a <= str1.length()-1; a++) {
if (result1.contains("" + str1.charAt(a))) { 
// charAt methodda you provide index number ve sana character olarak donuyor,
// If the string result does not contains str.CharAt(i), 
// then we concate it to the result. if it does we will not
   continue;
}
result1 += str1.charAt(a);
}
System.out.println(result1);
Posted by: Guest on June-04-2021

Code answers related to "remove duplicates from string java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language