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
bohemianguy100bohemianguy100 

filtering a list with duplicate values

I have a method that returns a Map<Id, List<Id>>

 

Here is the method:

 

private Map<Id, List<Id>> getDbrToAccountMap(Set<Id> dbrIds) {
    Map<Id, List<Id>> dbrAccountMap = new Map<Id, List<Id>>();
    List<Id> accountIds = new List<Id>();
    for(DBR_Group_Member__c member : [select Id, Contact__c, Contact__r.AccountId, DBR__c from DBR_Group_Member__c where DBR__c in: dbrIds]) {
        if(accountIds.isEmpty()) {
            accountIds = new List<Id>();
            dbrAccountMap.put(member.DBR__c, accountIds);
        }
        accountIds.add(member.Contact__r.AccountId);
    }
    return dbrAccountMap;
}

 

I only want to add to the list of accountIds for a unique DBR_c and unique account of the Contact_c

 

For example, if I have a DBR number of:

 

3333 and the contact is John Smith that is on the Acme account (add to the list)

next I have

3333 and the contact is Jane Smith that is on the Acme account (don't add to the list)

next I have

3333 and the contact is Bob Smith that is on the Big Company account (add to the list)

next I have

3333 and the contact is Doug Smith that is on the Big Company account (don't add to the list)

 

The DBR to account should be distinct.

 

In my method, I'm returning the accountIds, but they are not distinct based on the DBR.  I have to be careful because I don't want to remove a duplicate accountId in the list if the DBR number was different.  That would be a valid element in the list.

 

I was thinking instead of returning a Map<Id, List<Id>> I could return a Map<Id, Map<Id, List<Id>>> where the key to the outer map is the DBR__c and the value in the inner map.  The key to the inner map would be the contact and the value would be the list of account ids.  I was trying that, but couldn't get it to work correctly.

 

Any help would be appreciated.

Thanks.

liron169liron169

Hi,

 

Use a set and add this unique you need as a string to the set.

 

Something like this:

 

private Map<Id, List<Id>> getDbrToAccountMap(Set<Id> dbrIds) {
    Map<Id, List<Id>> dbrAccountMap = new Map<Id, List<Id>>();
    //List<Id> accountIds = new List<Id>();
    Set<String> uniqueSet=new Set<String>();

    for(DBR_Group_Member__c member : [select Id, Contact__c, Contact__r.AccountId, DBR__c from DBR_Group_Member__c where DBR__c in: dbrIds]) {
        if(!uniqueSet.contains(member.DBR__c + member.Contact__c))
    {
        uniqueSet.add(member.DBR__c + member.Contact__c)
        if(!dbrAccountMap.containsKey(member.DBR__c)
         dbrAccountMap.put(member.DBR__c, new List<id>);

        dbrAccountMap.get(member.DBR__c).add(member.Contact__r.AccountId);
    }
    }
    return dbrAccountMap;
}
ran67ran67
when you use the list there may chances of getting the duplicate values and better to use the set<id> inorder to avoid duplice after remove the duplicate values using the set<id> then you can add list to it.