+ Start a Discussion
Ashish KumarAshish Kumar 

How to count the number of occurrences of elements present in a List

Hi All,

Does anyone have any idea to how to count the number of occurrences of all the elements present in a list ? For Example, I have a list of names.
List is having 10 elements with values {A,B,C,D,A,B,A,A,A,A}. Now here A came 6 times, B came 2 times , C came 1 time and D also 1 time. But I don't know how to do it in apex.
Please help how can I get the count of repeated values in apex.
Best Answer chosen by Ashish Kumar
Bryan JamesBryan James

The first thing that comes to mind is if you were to use a map and loop through your array.

List<String> nameArray = new List<String>{'A','B','C','D','A','B','A','A','A','A'};
Map<String,Integer> elCount = new Map<String,Integer>();

for(String key : nameArray)
{
    if(!elCount.containsKey(key)){
    elCount.put(key,0);
    }
    Integer currentInt=elCount.get(key)+1;
    elCount.put(key,currentInt);
}


The result of this would look like:
{A=6, B=2, C=1, D=1}

If you are getting the names through a soql query you can use an AggregateResult List like this:
List<AggregateResult> ar = [Select Name, Count(Id) From SkinnyBeeCoding__Training_Seats__c Group By Name];
The result of this in my case is:
AggregateResult:{Name=Infantry July, expr0=6}, AggregateResult:{Name=Infantry June, expr0=1}​

All Answers

Bryan JamesBryan James

The first thing that comes to mind is if you were to use a map and loop through your array.

List<String> nameArray = new List<String>{'A','B','C','D','A','B','A','A','A','A'};
Map<String,Integer> elCount = new Map<String,Integer>();

for(String key : nameArray)
{
    if(!elCount.containsKey(key)){
    elCount.put(key,0);
    }
    Integer currentInt=elCount.get(key)+1;
    elCount.put(key,currentInt);
}


The result of this would look like:
{A=6, B=2, C=1, D=1}

If you are getting the names through a soql query you can use an AggregateResult List like this:
List<AggregateResult> ar = [Select Name, Count(Id) From SkinnyBeeCoding__Training_Seats__c Group By Name];
The result of this in my case is:
AggregateResult:{Name=Infantry July, expr0=6}, AggregateResult:{Name=Infantry June, expr0=1}​

This was selected as the best answer
sharathchandra thukkanisharathchandra thukkani
1) prepare  a set with list you have, alternatively we have set methods such as addAll which will convert list to set.
2) iterate through the set and take the list wih 10 elements as inner for loop.
3) compare each element of set with the list element and prepare a map<string,integer> where you store the count of respective elment.

sample code
//prepare set from the list.
Set<String> strSet = new Set<String>();
List<String> strList = new List<String>{A,B,C,D,A,B,A,A,A,A}
for(String str: strList ){
strSet.add(str);
}
//Map
Map<String, Integer> myMap = Map<String,Integer>();
//iterate through set

for(String str: strSet){
Integer countofChar = 0;
for(String strl: strList ){
   if(str == str1){
     countofChar++;
   }
}
myMap.put(strl, countofChar);
}
System.debug(myMap);
Vijayakumar KumarVijayakumar Kumar
Thank you sharath.  Great sample.  
sagar077sagar077
Hello, Please me on this requirement.

Count Account with Different Industry(on account picklist fields is there)(e,g. For Industry Electronics we have 3 Accounts) using Map.

plz help me i am new in apex collection.