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
ASHISH KUMAR YADAV 7ASHISH KUMAR YADAV 7 

how to get duplicate value in map and get unique vale and some

DuplicateMKT hold duplicate value like [65,65,5,5] i need to take the unique value and sum that value and put in map-outletIdByTotalmarket thisis map<sting,string> what is the good solution use map or set and do the calculation anyone can help.

//Code Started from here             
   Decimal totalmarket=0.0;
                   Decimal mkttotal=0.0;
                   List<String> DuplicateMKT =new List<String>();
                   List<String> noDupes;
                   for(String outletmktId : lstStagingByOutletId.keySet()){
                   List<SNR_Staging__c> currlist = lstStagingByOutletId.get(outletmktId);
                       for(SNR_Staging__c currrecords : currlist){
                           if(!String.isEmpty(currrecords.SNR_HCV_MARKET_R6M__c) ){ 
                               DuplicateMKT.add(currrecords.SNR_HCV_MARKET_R6M__c);
                               System.debug('DuplicateMKT-->'+DuplicateMKT);
                               noDupes = new List<String>(new Set<String>(DuplicateMKT));
                               System.debug('noDupes-->'+noDupes);
                           }
                       }
                       for(String s: noDupes){
                           totalmarket = totalmarket + Decimal.valueOf(s);
                           System.debug('totalmarket--->'+totalmarket);
                           mkttotal += Decimal.valueOf(s);
                           System.debug('mkttotal--->'+mkttotal);
                       }
                       outletIdByTotalmarket.put(outletmktId,mkttotal);
                       System.debug('outletIdByTotalmarket-->'+outletIdByTotalmarket);
                   }
//code end here

 
Prateek Prasoon 25Prateek Prasoon 25
Using a Set to store the unique values is a good approach since Sets do not allow duplicate values. You can then iterate through the Set to calculate the total market value and store it in a map.
Here's an updated version of the code using a Set and a Map:
Decimal totalmarket = 0.0;
Map<String, Decimal> outletIdByTotalmarket = new Map<String, Decimal>();
Set<String> uniqueMKT = new Set<String>();
for (String outletmktId : lstStagingByOutletId.keySet()) {
    List<SNR_Staging__c> currlist = lstStagingByOutletId.get(outletmktId);
    for (SNR_Staging__c currrecords : currlist) {
        if (!String.isEmpty(currrecords.SNR_HCV_MARKET_R6M__c)) { 
            uniqueMKT.add(currrecords.SNR_HCV_MARKET_R6M__c);
        }
    }
    for (String s : uniqueMKT) {
        totalmarket += Decimal.valueOf(s);
    }
    outletIdByTotalmarket.put(outletmktId, totalmarket);
    totalmarket = 0.0;
    uniqueMKT.clear();
}

If you find this answer helpful,Please mark it as the best answer,