【问题标题】:Find word duplicate in string without using Hashmap, HashSet etc. in Java在Java中不使用Hashmap、HashSet等查找字符串中的重复单词
【发布时间】:2017-09-27 07:49:35
【问题描述】:

我有下面的代码,它确实在没有 HashMap、HashSet 等的字符串中找到重复项,但我想要一个比这个更好的解决方案。 请帮助我是Java编程的新手。我相信 Java 足够强大,可以做到这一点 P.S-这并不是说我试图在 Java 集合中避免使用 HashMap 等。我只想要一个更时尚的解决方案

public class practice {
    static void countWords(String st){
         //split text to array of words
         String[] words=st.split("\\s");
       //frequency array
         int[] fr=new int[words.length];
       //init frequency array
         for(int i=0;i<fr.length;i++)
           fr[i]=0;
         //count words frequency
         for(int i=0;i<words.length;i++){
           for(int j=0;j<words.length;j++){
             if(words[i].equals(words[j])) 
               {
                 fr[i]++;

                    }
                }
               }

         //clean duplicates
           for(int i=0;i<words.length;i++){
             for(int j=0;j<words.length;j++){
               if(words[i].equals(words[j])) 
               {
                 if(i!=j) words[i]="";

               }
         }
         }

    //show the output

    int total=0;
    System.out.println("Duplicate words:");
    for(int i=0;i<words.length;i++){

    if(words[i]!=""){


    System.out.println(words[i]+"="+fr[i]);

    total+=fr[i];

    }    
       }

    System.out.println("Total words counted: "+total);
     }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
           countWords("apple banna apple fruit sam fruit apple hello hi hi hello hi");  
    }

}

【问题讨论】:

    标签: java string hashmap duplicates


    【解决方案1】:

    虽然 Hashmap 和 Hashset 最适合这个要求。但如果你不想使用它,你也可以更有效地实现同样的目标:

    1. 将句子拆分为数组,然后使用 Array.sort(); 按字母顺序(第一个字母)对其进行排序。 排序后,您可以遍历数组并以线性时间存储重复的单词数。
    2. 使用tries数据结构。

    【讨论】:

      【解决方案2】:

      您可以使用 Java8 streams 在一行代码中编写整个 countWords 方法(遵循内联 cmets):

      static void countWords(String st){
        Map<String, Long> wordsAndCounts = 
           Arrays.stream(st.split("\\s")).    //Splt the string by space i.e., word
               collect(Collectors.groupingBy( //Apply groupby
               Function.identity(),          //Map each word
               Collectors.counting()         //Count how many words
           ));
          System.out.println(wordsAndCounts);
      }
      

      输出:

      {banna=1, hi=3, apple=3, fruit=2, hello=2, sam=1}
      

      【讨论】:

        【解决方案3】:
        public static void main(String[] args) {
                String s = "abcabcc abc abcdeffrgh";
                char[] ch = s.toCharArray();
                String temp = "";
                int j = 0;
                for (int i = 0; i < s.length(); i++) {
                    int count = 0;
                    char result = 0;
                    for (j = 0; j < s.length(); j++) {
                        if (ch[i] == ch[j]) {
                            result = ch[i];
                            count = count + 1;
                        } else {
                            result = ch[i];
                        }
                    }
                    if (!temp.contains(Character.toString(ch[i]))) {
                        temp = temp + ch[i];
                        System.out.println(result + "--count--" + count);
                    }
        
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-16
          • 1970-01-01
          • 1970-01-01
          • 2021-06-30
          • 2017-10-21
          • 2021-02-18
          • 1970-01-01
          相关资源
          最近更新 更多