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
kavya mareedukavya mareedu 

Hello! I want to write a program to identify the duplicate values. I have a myList with all the list values and newList with set values. In set values the duplicates of list were removed. Compare the list and set,display the cmmn values in othr list

public class Colors {
    public string colors;
    public List<string> myList;
    public set<string> newList;
    
    public colors(){
        myList=new List<string>{'Blue','Blue violet','Red','Green','Dark Red','Light Red','Orange Red','Coral Red',
            'Forest Green','Hunter Green','Sage Green','Black','Red','Orange','Yellow','Blue violet'};
                system.debug(myList.size()); 
 newList=new set<string>();
        newList.addAll(myList);
        system.debug(newList);
}
kavya mareedukavya mareedu
I am not understanding how to write the logic. Please help me with this.
GauravendraGauravendra
Hi Kavya,

My understanding is you want to create a list of comman string among the list and set of string.
You can try something like this:
public class Colors {
public string colors;
public List<string> myList;
public set<string> newList;
public List<String> commList;

public colors(){
    myList=new List<string>{'Blue','Blue violet','Red','Green','Dark Red','Light Red','Orange Red','Coral Red',
    'Forest Green','Hunter Green','Sage Green','Black','Red','Orange','Yellow','Blue violet'};
    newList = new Set<String> {'Blue'};
    commList = new List<String>();
    for(String str : myList) {
        for(String st : newList) {
            if(str == st) 
                commList.add(str);
        }

    }
    system.debug(commList);
}
}

Hope this helps!
Martha VMartha V
public class Colors {
    public string colors;
    public List<string> myList;
    public set<string> newList;
    
    public colors(){
        myList=new List<string>{'Blue','Blue violet','Red','Green','Dark Red','Light Red','Orange Red','Coral Red',
            'Forest Green','Hunter Green','Sage Green','Black','Red','Orange','Yellow','Blue violet'};
                system.debug(myList.size()); 
 newList=new set<string>();
        newList.addAll(myList);   <---- here you just added all the colors from myList to the newList
        system.debug(newList);
}
next you have to loop through both arrays and compare each value of one list against all the values of the other string and add the value to another list when you find a value equal to the one you are testing. Because your list newList is a complete duplicate of your list myList all the values will be added to the list with duplicate values.

so basicallly you will use a for loop that cycles through one list and another for loop that cycles through the other list to compare values:
(add this after your code and befer the end curly-bracket of your class.
List<String> duplicatesList = new List<String>();
for (String compareIt : newList){
     for (String theValue : myList){
           if (compareIt == theValue){
               duplicatesList.add(compareIt);
               break;
           }           
      }
}




 
kavya mareedukavya mareedu

Hey! I wrote the whole code but I am not able to execute it

When I am trying to create an object in the anonymous window and call the method. It says: Line: 2, Column: 3
Method does not exist or incorrect signature: void colors() from the type Colors


Help me out:


public class Colors {
    public string colors;
    public List<string> myList;
    public set<string> newList;
    public List<String> commList;
    
    public colors(){
        myList=new List<string>{'Blue','Blue violet','Red','Green','Dark Red','Light Red','Orange Red','Coral Red',
            'Forest Green','Hunter Green','Sage Green','Black','Red','Orange','Yellow','Blue violet'};
                system.debug(myList.size()); 
        system.debug(myList);
        system.debug(myList.get(9));
        newList=new set<string>();
        commList = new List<String>();
        for(String str : myList) {
            for(String st : newList) {
                if(str == st) 
                    commList.add(str);
            }
        }
        system.debug(commList);
    }
    
public void sort(){
    myList.sort();
    system.debug(myList);
}
public void reverseorder(){
    List<String> finalList = new List<String>();
    for(Integer i = myList.size()-1; i>=0;i--)
    {
        finalList.add(myList.get(i));
    }
    System.debug('Check the Order -->'+finalList);
}

}


Colors c=new Colors();
c.colors();

Please let me know how should I fix this error.
I am just 4 months old with this development part. 
It would be great if you make me understand this 

 

Martha VMartha V
on the second line you have declared a variable with the name colors. I think the code is getting confused because you have the variable and then you have a method with the same name. You are not using the variable, so take it out.

There is also the problem that the newList doesn't have any values when you start looking for duplicates.
 
public class Colors {
    public string colors;        <---- Delete this line, you don't use the variable 
    public List<string> myList;
    public set<string> newList;
    public List<String> commList;
    
    public colors(){
        myList=new List<string>{'Blue','Blue violet','Red','Green','Dark Red','Light Red','Orange Red','Coral Red',
            'Forest Green','Hunter Green','Sage Green','Black','Red','Orange','Yellow','Blue violet'};
                system.debug(myList.size()); 
        system.debug(myList);
        system.debug(myList.get(9));
        newList=new set<string>();    <--- newList doesn't have any values
        commList = new List<String>();   
        for(String str : myList) {
            for(String st : newList) {    <--- this for will not excecute because there are no values
                if(str == st) 
                    commList.add(str);
            }
        }
        system.debug(commList);
    }
    
public void sort(){                   <----- this method is never called
    myList.sort();
    system.debug(myList);
}
public void reverseorder(){              <---- before reversign the order you should sort it
    List<String> finalList = new List<String>();
    for(Integer i = myList.size()-1; i>=0;i--)
    {
        finalList.add(myList.get(i));
    }
    System.debug('Check the Order -->'+finalList);
}

}