+ Start a Discussion

remove duplicate from list of object

Hi how can i remove duplicate from list of object ! i try like this but it's doesn't work 
public static List<State__c> getState(){
        List <State__c> first = [SELECT Id,Name,Child__r.Name FROM State__c];
        List<State__c> uniqueValue = new List<State__c>();
        Set<State__c> myset = new Set<State__c>();
        for (State__cs : first) {
            if (myset.add(s)) {
        return uniqueValue; 

my list is like this :
Deepali KulshresthaDeepali Kulshrestha
Hi Mohammed, 

Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();

You can do this by using Sets, by definition, contain no duplicates.  So, the first addAll() causes any dupes to overwrite themselves when going into the set.  The second addAll() just gets you back to a list.  If you don't need the result to be a list, you can omit that.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.