【问题标题】:Remove and mark duplicates in a string array删除和标记字符串数组中的重复项
【发布时间】:2015-09-30 12:18:48
【问题描述】:

我有这个

String array[] = {"test","testing again", "test"};

我想标记和删除重复项。这是我需要的输出:

2x test

testing again

有人可以帮我做这件事吗? 我已经尝试过使用 Set,但它似乎无法识别字符串何时已经存在。

这是我的代码:

Set addons = new HashSet<String>();
final String[] arr ={"test","testing again", "test"};
            for (int i = 0; i < arr.length; i++) {
                Log.d(TAG, "contains adding " + arr[i]);

                if (addons.contains(arr[i])) {
                    //never enters here
                    Log.d(TAG, "contains " + arr[i]);
                    addons.remove(arr[i]);
                    addons.add("2 x " + arr[i]);
                } else {
                    addons.add("1 x " + arr[i]);
                }
            }

【问题讨论】:

  • @agad 如果可行,你能提供一个代码示例的答案吗?
  • 使用调试器也是个好主意(在if 语句中查看您的设置)..
  • 如果您的集合包含"2 x test",它将无法匹配contains("test")。您可以使用Map&lt;String,Integer&gt; 来计算每个字符串的出现次数。
  • 通过这篇文章,您可以获得具有唯一元素的列表:stackoverflow.com/questions/14656208/array-of-unique-elements

标签: java android arrays string set


【解决方案1】:

你可以这样做:

String[] arr = { "test", "testing again", "test" };
HashMap<String, Integer> counter = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
    if (counter.containsKey(arr[i])) {
        counter.put(arr[i], counter.get(arr[i]) + 1);
    } else {
        counter.put(arr[i], 1);
    }
}
System.out.println("Occurrences:\n");
for (String key : counter.keySet()) {
    System.out.println(key + " x" + counter.get(key));
}

您的示例不起作用,因为当您找到一个新出现的单词时,您将其删除并替换为 2x [word] 之类的内容,当该单词再次出现时,contains(...) 将返回 false,因为它不是在集合中更长。

【讨论】:

    【解决方案2】:

    在 Java 8 中:

    Stream.of("test", "testing again", "test")
            .collect(groupingBy(Function.identity(), counting()))
            .forEach((str, freq) -> {
                System.out.printf("%20s: %d%n", str, freq);
            });
    

    【讨论】:

      【解决方案3】:

      试试这个:

       public static void main(String[] args) {
      
              Set<String> addons = new HashSet<>();
              final String[] arr = { "test", "testing again", "test","test","testing again" };
              int count = 0;
              for (int i = 0; i < arr.length; i++) {
      
                  for (int j = 0; j < arr.length; j++) {
                      if (arr[i].equals(arr[j])) {
                          count++;
                      }
                  }
      
                  addons.add(count + " x " + arr[i]);
                  count = 0;
              }
      
              System.out.println(addons);
      
          }
      

      输出:

      [2 x testing again, 3 x test]
      

      【讨论】:

        【解决方案4】:
        String[] arr ={"test","testing again", "test"};
        Map<String, Integer> results = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
          Log.d(TAG, "contains adding " + arr[i]);
          if (results.containsKey(arr[i])) {
              Log.d(TAG, "contains " + arr[i]);
              results.put(arr[i], results.get(arr[i]) + 1);
          } else {
              results.put(arr[i], 1);
          }
        }  
        

        【讨论】:

          【解决方案5】:

          试试下面的代码。

          String[] array ={"test","testing again","test"};
          Set<String> uniqueWords = new HashSet<String>(Arrays.asList(array));
          

          【讨论】:

            【解决方案6】:

            问题是您没有直接在 Set 中添加“test”,而是“1 x test”。

            所以你最好使用 Map 来保存字符串,以及它的出现次数。

                String[] array = { "test", "testing again", "test" };
                Map<String, Integer> addons = new HashMap<>();
            
                for (String s : array) {
                    System.out.println("Dealing with [" + s + "]");
                    if (addons.containsKey(s)) {
                        System.out.println("\tAlready contains [" + s  + "]");
                        addons.put(s, addons.get(s) + 1); // increment count of s
                    } else {
                        System.out.println("\tFirst time seeing [" + s  + "]");
                        addons.put(s, 1); // first time we encounter s
                    }
                }
            

            【讨论】:

              【解决方案7】:

              使用这个,其中 Map 的 Key 是 String 元素,Value 是该元素的 Count。

              public static void main(String[] args) {
                      String array[] = {"test","testing again", "test"};
              
                      Map<String, Integer> myMap = new HashMap<>();
              
                      for (int i = 0; i < array.length; i++) {
                          if (myMap.containsKey(array[i])) {
                              Integer count = myMap.get(array[i]);
                              myMap.put(array[i], ++count);
                          } else{
                              myMap.put(array[i], 1);
                          }
                      }
              
                      System.out.println(myMap);
                  }
              

              【讨论】:

                【解决方案8】:
                String array[] = {"test","testing again", "test"};
                Map<String, Integer> countMap = new HashMap<>();
                for (int i = 0; i<array.length; i++) {
                    Integer count = countMap.get(array[i]);
                    if(count == null) {
                        count = 0;
                    }
                    countMap.put(array[i], (count.intValue()+1));
                }
                System.out.println(countMap.toString());
                

                输出

                {'test'=2, 'testing again'=1}
                

                【讨论】:

                  【解决方案9】:

                  您可以使用 Guava 中的 Multiset。

                  String array[] = {"test","testing again", "test"};
                  Multiset<String> set = HashMultiset.create(Arrays.asList(array));
                  System.out.println(set);
                  

                  输出:

                  [test x 2, testing again]
                  

                  Multiset 基本上计算您尝试添加对象的次数。

                  for (HashMultiset.Entry<String> entry :set.entrySet()) {
                      System.out.println(entry.getCount() + "x " + entry.getElement());
                  }
                  

                  输出:

                  2x test 
                  1x testing again
                  

                  【讨论】:

                    【解决方案10】:

                    您可以使用自己的包含重复项的类:

                    class SetWithDuplicates extends HashSet<String> {
                    
                       private final Set<String> duplicates = new HashSet<>();
                    
                        @Override
                        public boolean add(String e) {
                           boolean added = super.add(e);
                           if(!added) {
                               duplicates.add(e);
                           }
                           return added;
                        }
                    
                        public Set<String> duplicates() {
                            return duplicates;
                        }
                    
                    }
                    

                    并像@Ganpat Kaliya 一样使用它:

                    String[] array ={"test","testing again","test"};
                    SetWithDuplicates <String> uniqueWords = new SetWithDuplicates(Arrays.asList(array));
                    SetWithDuplicates <String> duplicates = uniqueWords.duplicates();
                    

                    【讨论】:

                      猜你喜欢
                      • 2017-12-30
                      • 1970-01-01
                      • 2012-09-15
                      • 1970-01-01
                      • 2012-05-09
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2016-01-06
                      相关资源
                      最近更新 更多