Rahul
Rahul

Reputation: 149

Arraylist find the count of consecutive duplicate elements

I am trying to find the COUNT of repeated elements in an array list. for example if array named "answerSheerPacketList" list contains values like {20,20,30,40,40,20,20,20},i need to show output like {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");
}

when i executed above code i got output like {20=5,30=1,40=2} but i am trying to get a output like {20=2,30=1,40=2,20=3}.

Upvotes: 1

Views: 2280

Answers (4)

markvgti
markvgti

Reputation: 4619

What you need is basically frequency counting. The following code will do it with a single pass through your answerSheerPacketList array:

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");
}

Upvotes: 0

Manish Chandra Joshi
Manish Chandra Joshi

Reputation: 431

This is a classic problem of counting runs of consecutive elements in an array. I have renamed the array to arr in the code for brevity.

    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
        }
    }

Performance-wise, this approach is aysmptotically optimal and runs in O(n), where n is the number of elements in the array.

Upvotes: 2

corroborator
corroborator

Reputation: 321

  Set<Integer> distinctSet = new HashSet<>(answerSheerPacketList);
    HashSet<Integer,Integer> elementCountSet=new HashSet<>();
    for (Integer element: distinctSet) {
    elementCountSet.put(element,Collections.frequency(answerSheerPacketList, element));

    }

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520928

A simple approach here would be to just iterate the arraylist once, and then keep tallies as we go along:

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)}

Upvotes: 2

Related Questions