【问题标题】:Arraylist find the count of consecutive duplicate elementsArraylist 查找连续重复元素的计数
【发布时间】:2019-09-30 01:12:11
【问题描述】:

我正在尝试查找数组列表中重复元素的 COUNT。 例如,如果名为“answerSheerPacketList”列表的数组包含像{20,20,30,40,40,20,20,20} 这样的值,我需要显示像{20=2,30=1,40=2,20=3} 这样的输出。

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

for (String a : answerSheerPacketList) {

    Integer j = hm.getinsAnswerSheetId(a);
    hm.put(a, (j == null) ? 1 : j + 1);
}

    // displaying the occurrence of elements in the arraylist

for(Map.Entry<String, Integer> val : hm.entrySet()){

     System.out.println("Element " + val.getKey() + " " 
    "occurs" + ": " + val.getValue()+ " times");
}

当我执行上面的代码时,我得到了像 {20=5,30=1,40=2} 这样的输出,但我试图得到像 {20=2,30=1,40=2,20=3} 这样的输出。

【问题讨论】:

  • 问题:您实际想要的最终数据结构是什么?请注意,它不能是普通地图,因为同一个键会出现多次。或者,您只是想要控制台的输出?
  • 标题应该是“Arraylist 查找连续重复元素的数量”

标签: java arraylist


【解决方案1】:

这里有一个简单的方法是只对数组列表进行一次迭代,然后在我们进行时保持计数:

List<Integer> list = new ArrayList<>();
list.add(20);
list.add(20);
list.add(30);
list.add(40);
list.add(40);
list.add(20);
list.add(20);
list.add(20);

Integer curr = null;
int count = 0;
System.out.print("{");
for (int val : list) {
    if (curr == null) {
        curr = val;
        count = 1;
    }
    else if (curr != val) {
        System.out.print("(" + curr + ", " + count + ")");
        curr = val;
        count = 1;
    }
    else {
        ++count;
    }
}
System.out.print("(" + curr + ", " + count + ")");
System.out.print("}");

{(20, 2)(30, 1)(40, 2)(20, 3)}

【讨论】:

  • 这个函数有一个问题,列表为空时会显示{(null, 0)}
【解决方案2】:

这是一个计算数组中连续元素的 runs 的经典问题。为简洁起见,我在代码中将数组重命名为 arr

    int run = 1;
    for (int i = 0; i < n; ++i) {   // n is the size of array
        if (i + 1 < n && arr[i] == arr[i + 1]) {
            run++;        // increment run if consecutive elements are equal
        } else {
            System.out.println(arr[i] + "=" + run + ", ");
            run = 1;      // reset run if they are not equal
        }
    }

在性能方面,这种方法是渐进最优的,并且在 O(n) 中运行,其中 n 是数组。

【讨论】:

  • 太棒了!如何知道哪种类型的代码性能最佳?..
【解决方案3】:
  Set<Integer> distinctSet = new HashSet<>(answerSheerPacketList);
    HashSet<Integer,Integer> elementCountSet=new HashSet<>();
    for (Integer element: distinctSet) {
    elementCountSet.put(element,Collections.frequency(answerSheerPacketList, element));

    }

【讨论】:

    【解决方案4】:

    您需要的基本上是频率计数。以下代码将通过您的 answerSheerPacketList 数组进行一次传递:

    int[] answerSheerPacketList = // initialization
    
    Map<Integer, Integer> frequencyCount = new LinkedHashMap<>();
    for (int i : answerSheerPacketList) {
        Integer key = Integer.valueOf(i);
        if (frequencyCount.containsKey(key)) {
            frequencyCount.put(key, Integer.valueOf(frequencyCount.get(key) + 1));
        } else {
            frequencyCount.put(key, Integer.valueOf(1));
        }
    }
    
    for (Integer key : frequencyCount.keySet()) {
        System.out.println("Element " + key + " occurs: " + frequencyCount.get(key)
                           + " times");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      相关资源
      最近更新 更多