Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding differences in two lists

I am thinking about a good way to find differences in two lists

here is the problem:

Two lists have some strings where first 3 numbers/characters (*delimited) represent the unique key(followed by the text String="key1*key2*key3*text").

here is the string example:

AA1*1D*4*The quick brown fox*****CC*3456321234543~

where "*AA1*1D*4*" is a unique key

List1: "index1*index2*index3", "index2*index2*index3", "index3*index2*index3"

List2: "index2*index2*index3", "index1*index2*index3", "index3*index2*index3", "index4*index2*index3"

I need to match indexes in both lists and compare them.

  1. If all 3 indexes from 1 list match 3 indexes from another list, I need to track both string entries in the new list

  2. If there is a set of indexes in one list that don't appear in another, I need to track one side and keep an empty entry in another side. (#4 in the example above)

return the list

This is what I did so far, but I am kind of struggling here:

        List<String> Base = baseListCopy.Except(resultListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values(keep differences in lists)
        List<String> Result = resultListCopy.Except(baseListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values (keep differences in lists)

        List<String[]> blocksComparison = new List<String[]>(); //we container for non-matching blocks; so we could output them later

        //if both reports have same amount of blocks
        if ((Result.Count > 0 || Base.Count > 0) && (Result.Count == Base.Count))
        {
            foreach (String S in Result)
            {
                String[] sArr = S.Split('*');
                foreach (String B in Base)
                {
                    String[] bArr = B.Split('*');

                    if (sArr[0].Equals(bArr[0]) && sArr[1].Equals(bArr[1]) && sArr[2].Equals(bArr[2]) && sArr[3].Equals(bArr[3]))
                    {
                        String[] NA = new String[2]; //keep results
                        NA[0] = B; //[0] for base
                        NA[1] = S; //[1] for result
                        blocksComparison.Add(NA);
                        break;
                    }
                }
            }
        }

could you suggest a good algorithm for this process?

Thank you

like image 645
Andrew Avatar asked May 11 '26 12:05

Andrew


1 Answers

You can use a HashSet.

Create a HashSet for List1. remember index1*index2*index3 is diffrent from index3*index2*index1.

Now iterate through second list.

Create Hashset for List1.

foreach(string in list2)
{
    if(hashset contains string)
       //Add it to the new list.
}
like image 138
Sandeep Avatar answered May 14 '26 01:05

Sandeep