function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashwani PradhanAshwani Pradhan 

How to check frequency of item in List?

I have a list with duplicate elements like below -

List<Contact_c> corr = [SELECT Contact_r.Name, Id, Name From Contact_c];

Now in result of corr getting same Contact_r.Name multiple times and I need to show all those contacts whose occurrence in list > 1
If I group Contact_r.Name and try to count then list will only give contact one time which will force me to query in loop to get all and heap size issue occuring.

If I am trying to use Map to occurrence then again heap issue came.

So I have only one option left that any how in corr iteration I can check frequency of Contact_r.Name in corr and if its greater than 1 then show record else continue

Please help me how can we achieve under Apex.
Best Answer chosen by Ashwani Pradhan
Nayana KNayana K
// Get count
Map<String, Integer> mapNameToCount = new Map<String, Integer>();

for(Contact__c objCon : [SELECT Contact_r.Name, Id, Name From Contact_c])
{
	// if it containsKey means greater than 1 occurence
	if(mapNameToCount.containsKey(objCon.Contact_r.Name))
	{
		mapNameToCount.put(objCon.Contact_r.Name, mapNameToCount.get(objCon.Contact_r.Name));
	}
	// 1 occurence
	else
	{
		mapNameToCount.put(objCon.Contact_r.Name, 1);
	}
}

// If you debug map you will see Name with count.

OR
 
// If you just want to check if Contact_r.Name occurs >1 and perform some logic


Set<String> setStrName = new Set<String>();

for(Contact__c objCon : [SELECT Contact_r.Name, Id, Name From Contact_c])
{
	// if set it contains means greater than 1 occurence
	if(setStrName.contains(objCon.Contact_r.Name))
	{
		// Do your logic for greater than 1
	}
	// 1 occurence
	else
	{
		setStrName.add(objCon.Contact_r.Name);
	}
}

 

All Answers

Nayana KNayana K
// Get count
Map<String, Integer> mapNameToCount = new Map<String, Integer>();

for(Contact__c objCon : [SELECT Contact_r.Name, Id, Name From Contact_c])
{
	// if it containsKey means greater than 1 occurence
	if(mapNameToCount.containsKey(objCon.Contact_r.Name))
	{
		mapNameToCount.put(objCon.Contact_r.Name, mapNameToCount.get(objCon.Contact_r.Name));
	}
	// 1 occurence
	else
	{
		mapNameToCount.put(objCon.Contact_r.Name, 1);
	}
}

// If you debug map you will see Name with count.

OR
 
// If you just want to check if Contact_r.Name occurs >1 and perform some logic


Set<String> setStrName = new Set<String>();

for(Contact__c objCon : [SELECT Contact_r.Name, Id, Name From Contact_c])
{
	// if set it contains means greater than 1 occurence
	if(setStrName.contains(objCon.Contact_r.Name))
	{
		// Do your logic for greater than 1
	}
	// 1 occurence
	else
	{
		setStrName.add(objCon.Contact_r.Name);
	}
}

 
This was selected as the best answer
Ashwani PradhanAshwani Pradhan
Thanks Nayna, I have used second one. But still strugling with heap size isssue.