How to find the common string from list of strings in C#

UserName123 1 Reputation point
2022-03-30T08:22:52.88+00:00

I have a Dictionary as below
Dictionary<string, List<Structure>> myDic = new Dictionary<string, List<Structure>>();

string[] tests = new string[] { "test", "test2", "test3", "test4" };

foreach (string test in tests)
{
myDic.Add(value, new List<Structure>{ });
}

struct Structure
{
string name1;
string name2;
}

I have a foreach loop which is as mentioned below

foreach (KeyValuePair<string, List<Structure>> pair in myDictionary)
{
string key = pair.Key;
Console.WriteLine("The output Key is: " + key);
foreach (Structure item in pair.Value)
{
string value = item.name2;
Console.WriteLine("The output value is: " + value);
}
}

THE OUTPUT OF THIS FOR_EACH GOES LIKE BELOW:
The key prints test1, test2, test3, test4 as outputs // (foreach KeyValuePair<string, list<structure="">> pair in myDictionary)

and the value prints
A for test1 iteration
A, B for test2 iteration
A, C, D for test3 iteration
A, C, F for test4 iteration

Now since A is common for all the tests (which is test1, test2, test3 and test4)

I need my output over as
"The output key is: test1, the output value is A"
"The output key is: test2, the output value is A"
"The output key is: test3, the output value is A"
"The output key is: test4, the output value is A"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

If there is no common value for all the Keys(test1, test2, test3 and test4), then I need to throw an exception

That is, if the
The Key is: test1
The value is: A

The Key is: test2
The value is: B

The Key is: test3
The value is: C

The Key is: test4
The value is: D

In this case since, there is no common value in for all the Keys, I need to throw an exception

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

If there is 3 common values for keys (i.e., as mentioned below)
The Key is: test1
The value is: A
The value is: B

The Key is: test2
The value is: A
The value is: B
The value is: C

The Key is: test3
The value is: C
The value is: A

The Key is: test4
The value is: D

In this case, the output needs to be
The key is: test1 and the value is: A
The key is: test2 and the value is: A
The key is: test3 and the value is: A
The key is: test4 and the value is: D

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,448 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 23,961 Reputation points Microsoft Vendor
    2022-03-31T10:45:39.083+00:00

    @UserName123 , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

     Dictionary<string, List<Structure>> myDic = new Dictionary<string, List<Structure>>();  
      
            string[] tests = new string[] { "test1", "test2", "test3", "test4" };  
      
            foreach (string test in tests)  
            {  
                myDic.Add(test, new List<Structure> { });  
      
            }  
            myDic["test1"].Add(new Structure { name1 = "A" });  
            myDic["test1"].Add(new Structure { name1 = "B" });  
            myDic["test2"].Add(new Structure { name1 = "A" });  
            myDic["test2"].Add(new Structure { name1 = "B" });  
            myDic["test2"].Add(new Structure { name1 = "C" });  
            myDic["test3"].Add(new Structure { name1 = "C" });  
            myDic["test3"].Add(new Structure { name1 = "A" });  
            myDic["test4"].Add(new Structure { name1 = "D" });  
            List<List<string>> all=new List<List<string>>();  
            var result=myDic.Select(x => x.Value.Distinct());  
            foreach (KeyValuePair<string, List<Structure>> pair in myDic)  
            {  
                List<string> list = new List<string>();  
                foreach (Structure item in pair.Value)  
                {  
                    string value = item.name1;  
       
                    list.Add(value);  
                }  
                all.Add(list);  
            }  
            var comm= all.SelectMany(x => x).Distinct()  
                  .Where(x => all.Select(y => (y.Contains(x) ? 1 : 0))  
                  .Sum() >=3).ToList();  
            foreach (KeyValuePair<string, List<Structure>> pair in myDic)  
            {  
                string key = pair.Key;  
                Console.WriteLine("The output Key is: " + key);  
                if(comm.Count()==0)  
                {  
                    Console.WriteLine("throw the exception");  
                }  
                else  
                {  
                    if(pair.Value.Select(i=>i.name1).Intersect(comm).Any())  
                    {  
                        foreach (var item in comm)  
                        {  
                            Console.WriteLine("The output value is: " + item);  
                        }  
                    }  
                    else  
                    {  
                        foreach (var item in pair.Value)  
                        {  
                            Console.WriteLine("The output value is: " + item.name1);  
                        }  
                    }  
                      
                }  
            }  
    

    I tested the above three conditions, they all work well.

    The final condition result:

    188732-image.png

    Best regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments