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
Aratz GuerraAratz Guerra 

Create a List without repeating elements

Hello,

I have a List of Objects, for example:

myList = (Object1, Object2 , Object3, Object4)

where:
Object1.Name = 'NameA',
Object2.Name = 'NameB',
Object3.Name = 'NameA' and
Object4.Name='NameC'

I have to create a List of Names without repeated Names, and also I need a List that counts the number of the elements, in this example:

NonRepeatedNamesList = ('NameA','NameB','NameC');
CounterList = (2,1,1);

Can anybody help me please?




 
Best Answer chosen by Aratz Guerra
Aratz GuerraAratz Guerra
Hi Likesh

The code has the following error:
Method does not exist or incorrect signature: void contains(String) from the type Map<String,Integer>
The issue is coming because I´m using String method with Set. So I use the solution below:
 
List<sObject> objList = new List<sObject>();
Boolean exist = false;
Boolean firstTime= true;
Map<String,Integer> newMap = new Map<String,Integer>();
List<Integer> counterList = new List<Integer>();
Set<String>strSet= new Set<String>();

for(Object o: objList){
            strSet.add(a.Name);
            exists = false;
            if(firstTime){
                Integer total = 0;
                newMap.put(o.Name, ++total);
                firstTime = false;
            }
            else{
                for(String s: newMap.keySet()){                    
                    if(s.equalsIgnoreCase(o.Name)){
                        exists = true;
                        Integer total = newMap.get(a.Name);
                        newMap.put(o.Name, ++total);
                        break;                        
                	}                	
            	}
                
                if(!exists){
                    Integer total = 0;
                    newMap.put(o.Name, ++total);
                }
            }
        }
        
        for(Integer i: newMap.values()){
            counterList.add(i);
        }


 

All Answers

imrohitimrohit
Hi 
Aratz Guerra
try this i hope this solve your problem
List<sObject> objList = new List<sObject>();
Set<String> strSet= new Set<String>();
Map<String,Integer> newMap = new Map<String,Integer>();
List<Integer> counterList = new List<Integer>();
for(sObject obj: objList){
	strSet.add(obj.Name);
	if(!newMap.contains(obj.Name)){
		Integer total = 0;
		newMap.put(obj.Name, ++total);
	}
	else if(newMap.contains(obj.Name)){
		var total  = newMap.get(obj.Name);
		newMap.put(obj.Name, ++total);
	}
}
for(sObject obj: objList){
	if(newMap.contains(obj.Name))
	counterList.add(newMap.get(obj.Name));
}

 
Aratz GuerraAratz Guerra
Hi Likesh

The code has the following error:
Method does not exist or incorrect signature: void contains(String) from the type Map<String,Integer>
The issue is coming because I´m using String method with Set. So I use the solution below:
 
List<sObject> objList = new List<sObject>();
Boolean exist = false;
Boolean firstTime= true;
Map<String,Integer> newMap = new Map<String,Integer>();
List<Integer> counterList = new List<Integer>();
Set<String>strSet= new Set<String>();

for(Object o: objList){
            strSet.add(a.Name);
            exists = false;
            if(firstTime){
                Integer total = 0;
                newMap.put(o.Name, ++total);
                firstTime = false;
            }
            else{
                for(String s: newMap.keySet()){                    
                    if(s.equalsIgnoreCase(o.Name)){
                        exists = true;
                        Integer total = newMap.get(a.Name);
                        newMap.put(o.Name, ++total);
                        break;                        
                	}                	
            	}
                
                if(!exists){
                    Integer total = 0;
                    newMap.put(o.Name, ++total);
                }
            }
        }
        
        for(Integer i: newMap.values()){
            counterList.add(i);
        }


 
This was selected as the best answer