Logic need to get common values from a Dictionary(of int, List(of int )) [Answered]RSS

5 replies

Last post Oct 12, 2011 12:53 AM by Mastan Oli

• Mastan Oli

Contributor

3455 Points

912 Posts

Logic need to get common values from a Dictionary(of int, List(of int ))

Oct 07, 2011 12:28 AM|Mastan Oli|LINK

Hi,

I am looking a logic for below example. Using dictionary object, created object like

Example 1

```Dictionary<int, List<int>> myObj1 = new Dictionary<int, List<int>>();
myObj1.Add(1, new List<int>() { 221, 224 });
myObj1.Add(2, new List<int>() { 222, 18273, 224 });
myObj1.Add(3, new List<int>() { 224 });
myObj1.Add(4, new List<int>() { 226, 224, 221 });
myObj1.Add(5, new List<int>() { 10, 333, 50, 1, 29, 33, 19, 224, 221 });
myObj1.Add(6, new List<int>() { 224, 22, 192, 11111, 38 });```

Example 2

```           Dictionary<int, List<int>> myObj2 = new Dictionary<int, List<int>>();
myObj2.Add(1, new List<int>() { 221, 222 });
myObj2.Add(2, new List<int>() { 222, 223, 224 });
myObj2.Add(3, new List<int>() { 224 });
myObj2.Add(4, new List<int>() { 223, 222 });
myObj2.Add(5, new List<int>() { 200, 333, 222 });```

Example 3

```           Dictionary<int, List<int>> myObj3 = new Dictionary<int, List<int>>();
myObj3.Add(1, new List<int>() { 501 });
myObj3.Add(2, new List<int>() { 423, 22, 102 });
myObj3.Add(3, new List<int>() { 102, 381 });
myObj3.Add(71, new List<int>() { 411, 393 });```

Example 4

```           Dictionary<int, List<int>> myObj4 = new Dictionary<int, List<int>>();
myObj4.Add(1, new List<int>() { 10 });
myObj4.Add(2, new List<int>() { 20 });```

• Example 1, list<int> common value is 224, all lists having 224
• Example 2, list<int> common value is 222, but key 3 doesn’t have 222.
• Example 3, list<int> common value is 102, but key 1 and 71 don’t have 102.
• Example 4, list<int> there is no common value.

I having such kind of objects, just want to find common values from the list<int>, also wants to get keys which are not in the common. how to write a function, which get common values from the above list? Could anyone help me please? Hope you all are understand the question, if not, or not explain clearly please give me chance to explain more

Thanks for you time and solution :)

Mãsthän Oli
playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful
• umairaslam22

Contributor

2535 Points

842 Posts

Re: Logic need to get common values from a Dictionary(of int, List(of int ))

Oct 07, 2011 12:50 AM|umairaslam22|LINK

The logic here you have to built is that you have to iterate through all list one by one let say from (myObj1)

Take its two elements 221 and 224 and make a loop to iterate through remaining lists if found then store it in a temp variable or temp list

go for the loop as many object do you have .

and similarly in the mid you can put a check for key in it.

use nested loops for key value pair comparison. like this

// for your keys iteration

for (int mykey = 1 ; mykey <=6 ; mykey++)

{

// your logic here for all six values of lists and matching

List<int> toMatch = myObj1[mykey];

// toMatch has know the list one toMatch

similarly store the remaining in other variable list like this

}

Umair Aslam
MCP
Blog
Please remember to Mark as answer if any post help you , it help others to find right solution in less time
• Mastan Oli

Contributor

3455 Points

912 Posts

Re: Logic need to get common values from a Dictionary(of int, List(of int ))

Oct 07, 2011 12:59 AM|Mastan Oli|LINK

Thank you Umair Aslam, I'm sorry, could you explain me more please? possible to write method please????????????

Mãsthän Oli
playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful
• gerrylowry

Star

14297 Points

5797 Posts

Re: Logic need to get common values from a Dictionary(of int, List(of int ))

Oct 07, 2011 01:45 AM|gerrylowry|LINK

You could try something like this, in pseudo-code:

class DecompositionGroup
{
int DictionaryKey     {get; set;}
int DictionaryValue  {get; set;}
}

List<DecompositionGroup> dataForAnalysis

for each entry in myDictionary,

for each value in myDictionary entry

DecompositionGroup  oneMore ==> new DecompositionGroup

oneMore.DictionaryKey      =  key value
oneMore.DictionaryValue  =  value from list

/* ************************ */

Now you have a list dataForAnalysis like this from your myObj1:

1, 221
1, 224
2, 222
2, 18273
2, 224
3, 224
4, 226
4, 224
et cetera

From dataForAnalysis you can order on the second column values; you already know that myObj1 contains six lists of <int> values.

Once the values are ordered, you can easily count occurences of each distinct value.

5, 1
5, 10
.
.
1, 224
2, 224
3, 224
4, 224
5, 224
6, 224
4, 226
5, 333
.
.
.
2, 18273

You could then have a class like:

class SummaryGroup
{
int OccurenceCount     {get; set;}
int RelatedValue         {get; set;}
}

When processing your orderred list, you could build a List<SummaryGroup> and then order that List<SummaryGroup> by OccurenceCount.

For me, I'd probably take an approach like the one above.

TIMTOWTDI  =.   there is more than one way to do it

g.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• TechFriend

Member

656 Points

184 Posts

Re: Logic need to get common values from a Dictionary(of int, List(of int ))

Oct 10, 2011 06:09 AM|TechFriend|LINK

Hi, If you are needing common values within the dictionary values. You can get good idea from below logic. Just pass your object and get common integer in the values.

`static List<int> GetCommonIntegers(Dictionary<int, List<int>> myObject)    {        //Contains the common integers        List<int> commonValues = new List<int>();        //First Key in the list        int firstKey = myObject.First().Key;        //Total items in the dictionary        int totalItems = myObject.Count;        //Iterate through each integer in the first value of the dictionary object        foreach (var firstItem in myObject.First().Value)        {            //Find the integer value in all values of the dictionary object            var test = from m in myObject                       where m.Value.Contains(firstItem)                       select new { m.Key, m.Value };            //comparing total items in dictionary object and total matched items. If it is equal, then that integer is present in all list<int> values            if (totalItems == test.Count())            {                commonValues.Add(firstItem);            }        }        //Display common integers        Console.WriteLine("CommonIntegers for this object:");        foreach (int item in commonValues)        {           Console.WriteLine("Integer Value:{0}", item);                    }        if (commonValues.Count == 0)        {            Console.WriteLine("No common values.");        }        Console.WriteLine("\r\n");        return commonValues;    }`
Please Mark as Answer if this reply helps you :)
• Mastan Oli

Contributor

3455 Points

912 Posts

Re: Logic need to get common values from a Dictionary(of int, List(of int ))

Oct 12, 2011 12:53 AM|Mastan Oli|LINK

Thanks Gerry Lowry, I'll try as per your advice.

Thanks TechFriend, When I execute your code, it shows 224 from myObj1 only, it couldnot find anothers... anyhow seems good idea, let me modify as per my needs :)

Thanks again.

Mãsthän Oli
playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful