【问题标题】:count total number of duplicate word in a sentence [duplicate]计算一个句子中重复单词的总数[重复]
【发布时间】:2020-01-16 04:46:18
【问题描述】:

我想计算一个句子中重复单词或重复单词的总数。在这里,我可以打印单词但无法计算这些单词。

import java.util.*;
public class Duplicate {  
    
    public static void main(String[] args) {  
       
        String input = "Big black bug bit a big black dog on his big black nose";  
        //Scanner scanner = new Scanner(System.in);
         //System.out.println("Enter the sentence");
        
        //String input = scanner.nextLine();
        
        int count; 
        int counter=0;
         
        //Converts the string into lowercase  
        input = input.toLowerCase();  
          
        //Split the string into words using built-in function  
        String words[] = input.split(" ");  
          
        System.out.println("Duplicate words in a given string : ");   
        for(int i = 0; i < words.length; i++) {  
            count = 1;  
            for(int j = i+1; j < words.length; j++) {  
                if(words[i].equals(words[j])) {  
                    count++;  
                    //Set words[j] to 0 to avoid printing visited word  
                    words[j] = "0";  
                    counter=counter+1;
                    
                }  
            }  
              
            //Displays the duplicate word if count is greater than 1  
            if(count > 1 && words[i] != "0")  {
                System.out.println(words[i]);
            }
            
        } 
         
        System.out.println("Total Duplicate words in a given string : " +counter);
}  
}

我期望输出:--

给定字符串中的重复单词:大黑

给定字符串中的重复单词总数:2

输出如下:

给定字符串中的重复单词:大黑

给定字符串中的重复单词总数:10

总数显示为 10 而不是 2。

【问题讨论】:

标签: java


【解决方案1】:

尝试使用 HashMap 而不是 2 个 for 循环,如下所示

public static void main(String[] args) {  

    String input = "Big black bug bit a big black dog on his big black nose";  
    HashMap<String, Integer> dupCount = new HashMap<>();

       //Converts the string into lowercase  
    input = input.toLowerCase();  

    //Split the string into words using built-in function  
    String words[] = input.split(" ");  


    System.out.println("Duplicate words in a given string : ");   
    for(int i = 0; i < words.length; i++) { 
        if(dupCount.containsKey(words[i])){
            int cnt = dupCount.get(words[i]);
            cnt = cnt + 1;
            dupCount.put(words[i], cnt);        
        }else{
            dupCount.put(words[i], 0);
        }
    }

    for(Map.Entry<String, Integer> test : dupCount.entrySet()){
        if(test.getValue() > 1)  {
            System.out.println("Duplicate words in a given string : " +test.getKey() + " : " + test.getValue());
        }
    }
}

在这种情况下,每次出现重复时都会打印最后一条语句,您可以根据需要对其进行修改。

【讨论】:

    【解决方案2】:

    counter 的增量移到此处应该可以:

    if (count > 1 && !words[i].equals("0")) {
        counter++;
        System.out.println(words[i]);
    }
    

    而不是在第二个循环中。现在counter 将在您每次发现另一个重复项时递增。仅当您发现有重复的单词时,将counter 移动到下方才会增加它。

    也就是说,这种方法可以通过使用Map 来计算每个单词出现的次数来简化。

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 2017-10-22
      • 2013-09-21
      • 2013-12-09
      • 2023-03-11
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多