Remove duplicate values from a string in java

前端 未结 14 2598
刺人心
刺人心 2020-12-06 05:26

Can anyone please let me know how to remove duplicate values from

String s=\"Bangalore-Chennai-NewYork-Bangalore-Chennai\"; 

and output sh

相关标签:
14条回答
  • 2020-12-06 06:06
    public class RemDuplicateWordFromString {
    public static void main(String[] args) {
        String s1 = "Hello India Hello India Hello India Hello India";
        countWords(s1);
    }
    public static void countWords(String s1) {
        String[] s2 = s1.split(" ");
        for (int i = 0; i < s2.length; i++) {
            for (int j = i + 1; j < s2.length; j++) {
                if (s2[i].equals(s2[j])) {
                    if (i != j) {
                        s2[i] = "";
                    }
                }
            }
        }
        for (int i = 0; i < s2.length; i++) {
            if (s2[i] != "") {
                System.out.print(s2[i] + " ");
            }
    
        }
    
    }
    

    }

    0 讨论(0)
  • 2020-12-06 06:10

    Just the idea:

    1. parse the string and split the tokens using separator "-"
    2. load the tokens into a Collection
    3. iterate the Collection and erase duplicates
    4. use the result Collection to build the new string

    The most tricky part should be 3, but not impossible. If you use a Set, you can skip this step.

    EDIT maybe you can substitute 2&3 with a presence check before adding the element

    0 讨论(0)
  • 2020-12-06 06:10
    public static void main(String[] args) {
        String str="Bangalore-Chennai-Newyork-Bangalore-Chennai";
        String output="";
        String [] arr=str.split("-");
    
        LinkedHashSet<String> lhs=new LinkedHashSet<String>();
        for (int i = 0; i < arr.length; i++) {
            lhs.add(arr[i]);
        }
        for(String s:lhs){
            output=output+s+"-";
        }
    
        System.out.println(output);
    }
    
    0 讨论(0)
  • 2020-12-06 06:12

    Create array of string by spliting by - and then create a hashSet from it.

    String s="Bangalore-Chennai-NewYork-Bangalore-Chennai"; 
    String[] strArr = s.split("-");
    Set<String> set = new HashSet<String>(Arrays.asList(strArr));
    

    If you want back it as string array then do following:

    String[] result = new String[set.size()];
    set.toArray(result);
    

    Here is a sample code to do this:

    String s="Bangalore-Chennai-NewYork-Bangalore-Chennai"; 
    String[] strArr = s.split("-");
    Set<String> set = new LinkedHashSet<String>(Arrays.asList(strArr));
    String[] result = new String[set.size()];
    set.toArray(result);
    StringBuilder res = new StringBuilder();
    for (int i = 0; i < result.length; i++) {
        String string = result[i];
        if(i==result.length-1)
            res.append(string);
        else
            res.append(string).append("-");
    }
    System.out.println(res.toString());
    

    Output:-

    Bangalore-Chennai-NewYork
    
    0 讨论(0)
  • 2020-12-06 06:19
    wordsArray = s.split("-");
    List<String> wordsList = new Arrays.asList(wordsArray);
    Set<String>  wordsSet  = new LinkedHashSet<String>(wordsList);
    
    String[] noDuplicates = new String[wordsSet.size()];
    wordsSet.toArray(noDuplicates);
    
    0 讨论(0)
  • 2020-12-06 06:20

    A little late to the game, but I would simply use a HashMap. It's easy to understand and has quick lookups on the keys, might not be the best way but it's still a good answer IMO. I use it all the time when I need to format quick and dirty:

                        String reason = "Word1 , Word2 , Word3";
                        HashMap<String,String> temp_hash = new HashMap<String,String>();
                        StringBuilder reason_fixed = new StringBuilder();
                        //in:
                        for(String word : reason.split(",")){
                            temp_hash.put(word,word);
                        }
                        //out:
                        for(String words_fixed : temp_hash.keySet()){
                            reason_fixed.append(words_fixed + " , ");
                        }
                        //print:
                        System.out.println(reason_fixed.toString());
    
    0 讨论(0)
提交回复
热议问题