+ Start a Discussion
MaheemSamMaheemSam 

How to remove duplicate values in the list

Hi,

 I need a suggestion how to remove duplicate values in the list I wrote a method which will display value from SOQL query return now there are lot of duplicate values which need to be removed and display only the unique values 

 Please suggest me how to remove the duplicate values in the list. 

Method 
/* Display product category select list */
  public List<SelectOption> ProdCatList
    {
    get
        {
           Set<String> setprocat = new Set<String>(); 
          
            ProdcatLst = [Select PricebookEntry.Product2.Category__c FROM OpportunityLineItem WHERE OpportunityId = :PageID];
            
            ProdCatList = new List<SelectOption>();
            
            for(OpportunityLineItem opl: ProdcatLst )
            {
                ProdCatList.add(new SelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c));
                setprocat.add(opl.PricebookEntry.Product2.Category__c);
            }
            return ProdCatList ;
        }
        set;
    }
Visual force 
<apex:selectOptions value="{!ProdCatList}"></apex:selectOptions>
                 </apex:selectList>
                <apex:inputText id="prodcatdiscount" value="{!prodcatdiscount }" label="Product Discount"/>


 
Best Answer chosen by MaheemSam
saikrishna.Gsaikrishna.G
try this one 

            for(OpportunityLineItem opl: ProdcatLst )
            {
              if(!setprocat.contains(opl.PricebookEntry.Product2.Category__c))
                ProdCatList.add(newSelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c));                                       setprocat.add(opl.PricebookEntry.Product2.Category__);
            }
            return ProdCatList ;

All Answers

saikrishna.Gsaikrishna.G

HI Sudhir,

rather than using a list you can use maps 

declare as map<string,string> ProdCatmap= new map<string,string>(); and add this to options .

 for(OpportunityLineItem opl: ProdcatLst ) {
      ProdCatmap.put(pl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c));
            ProdCatList.add(newSelectOption(ProdCatmap))  
                     setprocat.add(opl.PricebookEntry.Product2.Category__c);

}
return ProdCatList ;

MaheemSamMaheemSam
Thanks for you reply saikrishna I am getting incorrect signature eror for ProdCatList.add(newSelectOption(ProdCatmap))  

  Please find error below 

Error: Compile Error: Method does not exist or incorrect signature: newSelectOption(Map<String,String>) 
EldonEldon
Hi Sudhir
You reate one more list called UniqProdCatList. Loop through ProdCatList and then inside the loop you write a for loop to loop through UniqProdCatList (if UniqProdCatList .length>0) and add the sobjects to UniqProdCatList  If ProdCatList  sobject is not there in the UniqProdCatList and return UniqProdCatList .

code would be something like this,
/* Display product category select list */
    public List<SelectOption> ProdCatList
    {
        get
        {
            Set<String> setprocat = new Set<String>(); 
            
            ProdcatLst = [Select PricebookEntry.Product2.Category__c FROM OpportunityLineItem WHERE OpportunityId = :PageID];
            
            ProdCatList = new List<SelectOption>();
            
            for(OpportunityLineItem opl: ProdcatLst )
            {
                ProdCatList.add(new SelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c));
                setprocat.add(opl.PricebookEntry.Product2.Category__c);
            }
            
            boolean flag=false;
            UniqProdCatList = new List<SelectOption>();
            for(OpportunityLineItem opp: ProdCatLst ){
                flag=false;
                If(UniqueProdCatLst .length>0){
                    for(OpportunityLineItem UniqProdCatList ) {
                        if(Uopp.Name==opp.Name){
                            flag=true;
                        }
                    } 
                }
                if(flag!=true){
                    UniqProdCatList .add(opp);
                } 
            }
            return UniqProdCatList ;
        }
    }

Pls mark as solved if it helps you

Regards
MaheemSamMaheemSam
Hi Eldon, 

  I created another list as you suggested  

    List<SelectOption> UniqProdCatList  = new List<SelectOption>();  


  I get error in for loop please find below also i cannot use If(UniqueProdCatLst .length>0) 

Error: Compile Error: Loop variable must be of type System.SelectOption

Thanks
Sudhir
saikrishna.Gsaikrishna.G
try this one 

            for(OpportunityLineItem opl: ProdcatLst )
            {
              if(!setprocat.contains(opl.PricebookEntry.Product2.Category__c))
                ProdCatList.add(newSelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c));                                       setprocat.add(opl.PricebookEntry.Product2.Category__);
            }
            return ProdCatList ;
This was selected as the best answer