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
Arpit Gupta 40Arpit Gupta 40 

How to match two list of string and get miss match value ?

Hi,

I have two list of String ids, i need to compare two list of strings and put unique string in a Third List. How to do it.

suppose
List<String> ids1 = new List<String>('003aaa','003bbb','003ccc');
List<String> ids2 = new List<String>('003aaa','003bbb');
List<String> ids3 = new List<String>();

I want unique value in the third list :

ids3 = '003ccc';
Best Answer chosen by Arpit Gupta 40
Ajay K DubediAjay K Dubedi
Hi Arpit,

You can get unique values using below code:
 
public class Tset_Class 
{
    public static void Test_method()
    {
        List<String> ids1 = new List<String>{'003aaa','003bbb','003ccc'};
        List<String> ids2 = new List<String>{'003aaa','003bbb','004bde'};
        List<String> ids3 = new List<String>();
        for(String str : ids1)
        {
            if(!ids2.contains(str))
            {
                ids3.add(str);
            }
        }
        for(String str : ids2)
        {
            if(!ids1.contains(str))
            {
                ids3.add(str);
            }
        }
        System.debug(ids3);
    }
}

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

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Soyab HussainSoyab Hussain
Hi Arpit Gupta,

You can you this code right away to resolve your problem.
List<String> firstStringList = new List<String> {'a', 'a', 'b', 'c'}; //first string list
List<String> secongStringList = new List<String> {'a', 'a', 'b', 'c'}; //second string list
firstStringList.addAll(firstStringList); // merge the lists
List<String> uniqueStringList = new List<String>(new Set<String>(firstStringList)); // convert the merged list to a set to remove duplicate
System.debug('setString' + uniqueStringList);

If you found it useful please appreciate my efforts and mark it as the best answer

Regards,
Soyab
Ajay K DubediAjay K Dubedi
Hi Arpit,

You can get unique values using below code:
 
public class Tset_Class 
{
    public static void Test_method()
    {
        List<String> ids1 = new List<String>{'003aaa','003bbb','003ccc'};
        List<String> ids2 = new List<String>{'003aaa','003bbb','004bde'};
        List<String> ids3 = new List<String>();
        for(String str : ids1)
        {
            if(!ids2.contains(str))
            {
                ids3.add(str);
            }
        }
        for(String str : ids2)
        {
            if(!ids1.contains(str))
            {
                ids3.add(str);
            }
        }
        System.debug(ids3);
    }
}

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

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Arpit,

I went through the problem please refer bellow code.
 
public class Testabc {
       public static void Tesd()
    {
        List<String> str1 = new List<String>{'1','2','3'};
        List<String> str2 = new List<String>{'3','2'};
        List<String> str3 = new List<String>();
        for(String str : str1)
        {
            if(!str2.contains(str))
            {
                str3.add(str);
            }
        }
        System.debug(str3);
    }
}


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
www.kdeepali.com
Arpit Gupta 40Arpit Gupta 40
@Deepali Kulshrestha your answer is correct too but the first one get the hamper :)