【问题标题】:Using a Hashmap to detect duplicates and count of duplicates in a list使用 Hashmap 检测列表中的重复项和重复项计数
【发布时间】:2021-11-02 10:01:55
【问题描述】:

我正在尝试使用哈希映射来检测给定列表中的任何重复项,如果有,我想在该字符串中添加“1”以指示其重复项。如果它出现 3 次,第三次将在该字符串后添加“3”。

我似乎无法弄清楚,跟踪重复的数量。它只会将 1 添加到重复项中,无论是第 2 次、第 3 次还是第 4 次,..etc 重复。

这就是我所拥有的:

public static List<String> duplicates(List<String> given) {
     List<String> result = new ArrayList<String>();
     HashMap<String, Integer> hashmap = new HashMap<String, Integer>();

     for (int i=0; i<given.size(); i++) {
       String current = given.get(i);
       if (hashmap.containsKey(current)) {
           result.add(current+"1");
       } else {
           hashmap.put(current,i);
           result.add(current);
       }
     }

     return result;
 }

我也想包含只出现一次的值,原样(无串联)。

示例输入:["mixer", "toaster", "mixer", "mixer", "bowl"]

示例输出:["mixer", "toaster", "mixer1", "mixer2", "bowl"]

【问题讨论】:

  • 你能分享一个示例输入和你想要得到的结果吗?
  • 我已编辑我的帖子以包含该内容
  • final 变量不应该这样使用。将arraylist重命名为其他名称

标签: java hashmap duplicates


【解决方案1】:
public static List<String> duplicates(List<String> given) {
    final Map<String, Integer> count = new HashMap<>();
    return given.stream().map(s -> {
        int n = count.merge(s, 1, Integer::sum) - 1;
        return s + (n < 1 ? "" : n);
    }).collect(toList());
}

【讨论】:

    【解决方案2】:

    我将final重命名为output,因为第一个是关键字,不能用作变量名。

    if (hashmap.containsKey(current)) {
        output.add(current + hashmap.get(current)); // append the counter to the string
        hashmap.put(current, hashmap.get(current)+1); // increment the counter for this item
    } else {
        hashmap.put(current,1); // set a counter of 1 for this item in the hashmap
        output.add(current);
    }
    

    【讨论】:

      【解决方案3】:

      您总是添加硬编码字符串“1”,而不是使用地图中保存的计数:

      public static List<String> duplicates(List<String> given) {
          List<String> result = new ArrayList<>(given.size());
          Map<String, Integer> hashmap = new HashMap<>();
      
          for (String current : given) {
              if (hashmap.containsKey(current)) {
                  int count = hashmap.get(current) + 1;
                  result.add(current + count);
                  hashmap.put(current, count);
              } else {
                  hashmap.put(current, 0);
                  result.add(current);
              }
          }
      
          return result;
      }
      

      【讨论】:

        【解决方案4】:
        ArrayList finallist = new ArrayList<String>();
            
            for (int i=0; i<given.size(); i++) {
               String current = given.get(i);
               if (hashmap.containsKey(current)) {
                    hashmap.put(current,hashmap.get(current)+1);
               } else {
                   hashmap.put(current,1); 
                         
               }
               String num = hashmap.get(current) == 1 ? "" :Integer.toString(hashmap.get(current));
               finallist.add(current+num);
             }
             
             System.out.println(finallist);
        

        【讨论】:

          猜你喜欢
          • 2021-01-02
          • 2019-02-04
          • 2023-04-08
          • 1970-01-01
          • 2020-08-14
          • 1970-01-01
          • 2020-04-05
          • 1970-01-01
          • 2016-03-31
          相关资源
          最近更新 更多