【问题标题】:Java count occurrence of each item in an sorted arrayJava计数排序数组中每个项目的出现次数
【发布时间】:2013-05-24 23:22:51
【问题描述】:

我有一个字符串数组,想计算任何单个字符串的出现次数。

我已经整理好了。 (这是一个长数组,我想摆脱 O(n²) 循环)

这是我的代码.. 显然它在 ind.outOfB 中用完。 exc..原因很清楚,但我不知道如何解决..

for (int i = 0; i < patternsTest.length-1; i++) {
        int occ=1;
        String temp=patternsTest[i];
        while(temp.equals(patternsTest[i+1])){
            i++;
            occ++;
        }
    }

【问题讨论】:

  • 为什么不使用Map&lt;String, Integer&gt;
  • 我需要原始计数.. 我不知道我是否会为此创建地图...
  • 你为什么不呢?以后修改会更快,更容易。
  • 您不想使用地图来提高效率吗?通过排序,你会失去很多效率,使用地图意味着你不需要预先排序。但如果你真的不想使用地图,请解释原因,或者我想就这么说吧:)

标签: java arrays find-occurrences


【解决方案1】:

这将是一个 HashMap 的好地方,键是 Word,值是它出现的次数。 Map.containsKeyMap.get 方法是常数时间查找,速度非常快。

Map<String,Integer> map = new HashMap<String,Integer>();
for (int i = 0; i < patternsTest.length; i++) {
    String word=patternsTest[i];
    if (!map.containsKey(word)){
        map.put(word,1);
    } else {
        map.put(word, map.get(word) +1);
    }
}

作为一个附带好处,您甚至不需要事先排序!

【讨论】:

  • 这是一个很好的答案,但是我会将声明更改为父类 Map map = new HashMap(); amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683
  • 但是 .containsKey() 会遍历整个地图吗?并且每次都会覆盖现有条目..?这种方式似乎效率低下..并不是说这是一种不好的方法..
  • containsKey 是 O(1) 搜索。这意味着它不会遍历整个地图,它更类似于索引到数组而不是完整搜索。我也会用这个更新答案。
  • 您希望效率如何?如果你想让我把那个版本而不是这个版本,你可以用一行额外的代码来稍微加快速度
  • 好问题:P .. 每个人都在寻找最快的,对吧? ;) 到目前为止已经足够了.. 我认为我的方法的工作版本会更慢.. 有所有的排序和 bla。非常感谢:)
【解决方案2】:

你可以使用Java HashMap:

Map<String, Integer> occurrenceOfStrings = new HashMap<String, Integer>();

for(String str: patternsTest)
{
    Integer currentValue = occurrenceOfStrings.get(str);
    if(currentValue == null)
        occurrenceOfStrings.put(str, 1);
    else
        occurrenceOfStrings.put(str, currentValue + 1);
}

【讨论】:

    【解决方案3】:

    这没有超出范围的索引:

    String[] patternsTest = {"a", "b"};
    for (int i = 0; i < patternsTest.length-1; i++) {
        int occ=1;
        String temp=patternsTest[i];
        while(temp.equals(patternsTest[i+1])){
            i++;
            occ++;
        }
    }
    

    您可以通过将数据更改为:

    String[] patternsTest = {"a", "a"};
    

    【讨论】:

      【解决方案4】:

      你可以尝试一张地图,只有一个循环

      Map<String, Integer> occurences = new HashMap<String, Integer>();
      String currentString = patternsTest[0];
      Integer count = 1;
      
      for (int i = 1; i < patternsTest.length; i++) {
          if(currentString.equals(patternsTest[i]) {
              count++;
          } else {
              occurrences.put(currentString, count);
              currentString = patternsTest[i];
              count = 1;
          }
      }
      occurrences.put(currentString, count);
      

      【讨论】:

        【解决方案5】:

        Guava Multiset解决方案(两行代码):

        Multiset<String> multiset = HashMultiset.create();
        multiset.addAll(Arrays.asList(patternsTest));
        
        //Then you could do...
        multiset.count("hello");//Return count the number of occurrences of "hello".
        

        我们可以同时使用已排序和未排序的数组。易于维护代码。

        【讨论】:

          【解决方案6】:

          我的解决办法是:

          public int cantOccurences(String pattern, String[] values){
            int count = 0;
          
            for (String s : values) {
              count +=  (s.replaceAll("[^".concat(pattern).concat("]"), "").length());
            }
          return count;
          }
          

          【讨论】:

            猜你喜欢
            • 2011-12-27
            • 1970-01-01
            • 2012-07-23
            • 2014-10-16
            • 1970-01-01
            • 1970-01-01
            • 2014-03-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多