【问题标题】:return duplicates from an array in efficient way in java [duplicate]在java中以有效的方式从数组中返回重复项[重复]
【发布时间】:2014-06-17 06:57:34
【问题描述】:

我想返回一个数组中的重复项。

int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3};

我使用下面的方法返回重复。

private static Set<Integer> checkDuplicate(int[] intArray) {
    Set<Integer> values = new HashSet<>();

    for (int i = 0; i < intArray.length - 1; i++) {
        if (intArray[i] == (intArray[i + 1])) {
            values.add(intArray[i]);
        }

        else
            System.out.println("not equal");
    }

    return values;
}

但是以这种方式它只检查结果值。这需要大量的比较和耗时。那么有没有更好的方法呢?

【问题讨论】:

标签: java collections


【解决方案1】:

如果不想使用散列(HashSet 或 HashMap),可以先对数组进行排序。然后,您可以通过检查连续值来查找重复项。总的来说,这种方法的时间复杂度为 O(n log n)。

【讨论】:

  • +1.. 很有意义..
【解决方案2】:

你可以试试这个example:

import java.util.*;
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3, 4, 7, 5, 4};
        Set<Integer> duplicates = checkDuplicate(strArray);
        System.out.printf("Duplicates: %s\n", duplicates);
    }
    private static Set<Integer> checkDuplicate(int[] intArray)
    {
        Set<Integer> duplicates = new HashSet<Integer>();
        Set<Integer> tmp = new HashSet<Integer>();
        for(Integer i: intArray)
        {
            if(!tmp.add(i))
            {
                duplicates.add(i);
            }
        }
        return duplicates;
    }
}

【讨论】:

    【解决方案3】:

    如果您正在寻找效率,那么 使用HashMap,它有O(1) speed,同时迭代enhanced forloop中的整数数组,因为它比普通的forloop略快

     private static Set<Integer> checkDuplicate(int[] intArray) {
        HashMap<Integer,Integer> values = new HashMap<Integer,Integer>();
        Set<Integer> values2 = new HashSet<Integer>();
    
    
        for(Integer i : intArray) //0(n)
        {
            if(values.get(i) != null) //O(1)
                values2.add(i);
            else
                values.put(i, i);
        }
    
    
        return values2;
    }
    

    【讨论】:

      【解决方案4】:

      扫描您的输入并将每个数字及其计数存储在Map&lt;Interger, Integer&gt; 中。然后遍历Map并将所有值> 1的键放入生成的Set

      参见此处:https://stackoverflow.com/a/15217535/461499

      【讨论】:

        【解决方案5】:

        你必须使用Collections.frequency

        它只使用equals方法。如果您使用任何集合 Map,Set,List 使用 equals 方法比较两个对象,以及 Has 集合使用 hashCode 方法,这需要更多的处理时间。

        【讨论】:

          【解决方案6】:
          public static void main(String[] args) {
                  int[] arr = { 1, 2, 3, 1, 4, 4, 1, 5 };
                  // System.out.println(Arrays.toString(arr));
                  List<Integer> l = new ArrayList<Integer>();
                  for (int i = 0; i < arr.length; i++) {
                      l.add(i, arr[i]);
                  }
                  // System.out.println(l);
                  Set<Integer> set = new HashSet<Integer>();
                  for (int j = 0; j < l.size(); j++) {
                      if (Collections.frequency(l, l.get(j)) > 1) {
                          set.add(l.get(j));
                      }
                  }
          
                  System.out.println(set);
          
              }
          
          O/P :
          [1, 4]
          

          【讨论】:

          • 这只会给出每个重复项的计数,而不是值。
          • 我想返回有重复的值。不是重复的频率。
          • @kkk - 检查我编辑的答案..
          • 这具有 O(n^2) 时间复杂度,OP 试图避免。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-16
          • 1970-01-01
          • 2015-11-07
          • 2018-10-15
          • 1970-01-01
          • 2013-02-25
          相关资源
          最近更新 更多