【问题标题】:Get unique indexes after sorting an array with duplicate entries对具有重复条目的数组进行排序后获取唯一索引
【发布时间】:2013-04-12 19:24:35
【问题描述】:

我需要对数组进行排序并获取链接到未排序数组的索引。问题是如果未排序的数组包含重复的条目,即[1,1,1,2,2,],那么这些条目的索引是相同的。对于示例[3,5,5,3,3,],索引将是[0,1,1,0,0]。但我需要获得以下索引[0,3,4,1,2]。如何做到这一点?

ArrayList<Double> nfit = new ArrayList<Double>();
ArrayList<Double> nfit_copy = new ArrayList<Double>(nfit);

// Fill nfit

Collections.sort(nfit);

int[] ind = new int[nfit.size()];

for (int n = 0; n < nfit.size(); n++){
    ind[n] = nfit_copy.indexOf(nfit.get(n));
}

【问题讨论】:

    标签: java sorting indexing


    【解决方案1】:

    这是我使用SetHashMap 结构的解决方案。请阅读代码 cmets 了解更多信息。

    int[] input = { 45, 3, 4, 9, 2, 1, 45, 3 };
    // Backup the initial array for later use.
    int[] originalArray = Arrays.copyOf(input, input.length);    
    Arrays.sort(input);
    
    // Determine all occurences with a set.
    Set<Integer> set = new HashSet<Integer>();
    for (int i : input) {
        set.add(i);
    }
    
    // Populate a hashmap for keeping <value,index> pairs.
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int counter = 0;
    for (int i : set) {
        map.put(i, counter++);
    }
    
    // Populate the output array.
    int[] output = new int[input.length];
    for (int i = 0; i < output.length; i++) {
        output[i] = map.get(originalArray[i]);
    }
    

    就是这样。如果将output的内容打印到控制台,就可以看到结果了。

    【讨论】:

      【解决方案2】:

      这是因为你使用了nfit_copy.indexOf(nfit.get(n));,如果你有重复的号码,它会给你相同的索引

      举例

      [3,5,5,3,3,]---->每次你使用:nfit.indexOf(3),都会给你索引0

      所以,您可能需要更改此值或将其设置为 null(不要删除它,因为它会更改索引),以允许您获取下一个重复数字

      试试这个:

      ArrayList<Integer> nfit = new ArrayList<Integer>();
      ArrayList<Integer> nfit_copy = new ArrayList<Integer>(nfit);
      
      // Fill nfit 
      
        nfit.add(3);
        nfit.add(5);
        nfit.add(5);
        nfit.add(3);
        nfit.add(3);
      
      
        nfit_copy = (ArrayList<Integer>) nfit.clone();
      
      
        Collections.sort(nfit);
      
        int[] ind = new int[nfit.size()];
      
        for (int n = 0; n < nfit.size(); n++) {
               ind[n] = nfit_copy.indexOf(nfit.get(n));
               nfit_copy.set(nfit_copy.indexOf(nfit.get(n)), null);
         }
         for (int i = 0; i < ind.length; i++) {
               int j = ind[i];
               System.out.println(j);
          }
      

      【讨论】:

      • 它为 nfit.add(3) 提供了不正确的结果; nfit.add(3); nfit.add(5); nfit.add(3); nfit.add(3);。结果是 0 1 3 4 2,而不是 0 1 4 2 3。
      • 如何0 1 4 2 3 ??您在索引中的编号为 3 (0 ,1 ,3 ,4 ),在索引中的编号为 5 2 ,所以请再次检查:)
      • 可能我误解了你的想法。我检查了 Tee Gee 的代码(见下文),它为数组 [3,3,5,3,3] 提供了 0 1 4 2 3,这对我来说似乎是正确的。
      • 0 1 4 2 3 将意味着您的数组在排序后是3 3 3 5 3,所以这不是您想要的排序索引,对我来说似乎不正确:)
      • 我们在谈论不同的事情。在您的情况下,您将索引分配给未排序的数组(抱歉,这正是我需要的),但 Tee Gee 提供了排序索引。
      【解决方案3】:
      public static void main(String[] args) {
              Integer[] input = new Integer[]{1,2,2,1,5,6,2,3,2,3,4,5,6,1};
              List<Integer> mappedIndexes = new ArrayList<Integer>();
      
              List<Integer> sorted = new ArrayList<Integer>();
              for (int num : input) {
                  sorted.add(num);
              }       
              Collections.sort(sorted);
      
              for (int number : input) {
                  System.out.println("finding for input -> " + number);
                  for (int i = 0; i < sorted.size(); i++) {
                      int sortedNumber = sorted.get(i);
                      if (number == sortedNumber) {
                          if (mappedIndexes.contains(i)) {
                              continue;
                          } else {
                              System.out.println("setting index as -> " + i);
                              mappedIndexes.add(i);
                              break;
                          }
                      }
                  }
              }
              System.out.println(mappedIndexes);
          }
      

      【讨论】:

        猜你喜欢
        • 2016-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-22
        • 2017-04-28
        • 1970-01-01
        相关资源
        最近更新 更多