The problem is:
Given two sorted lists, L1 and L2, then how i write a procedure to compute L1 n L2 (intersection) ?
can anyone help me , please?

Recommended Answers

All 9 Replies

With the help of LINQ this is easy:

static void Main(string[] args)
        {
            string[] first = { "John", "Dave", "Mary" };
            string[] second = { "Danny", "Nicole", "Mary" , "James"};

            IEnumerable<string> intersect = first.Intersect(second);
            Console.WriteLine("*** First array:");
            foreach (string name in first)
                Console.WriteLine(name);
            Console.WriteLine("*** Second array:");
            foreach (string name in second)
                Console.WriteLine(name);
            Console.WriteLine("*** Intersection:");
            foreach (string name in intersect)
                Console.WriteLine(name);
            Console.ReadKey();
        }
commented: Linq is the best option :) +2

Ok, sortedList, what is the Key are Value type? Even the Linq query can deffer a lot, depending on the types.
Is it somethink like: SortedList<string, string> or do you use any custom objects inside?

Mitja

commented: Could not give rep to your next post so I do it here. +9

I did an exampe with sortedList:

private Method()
        {
            Win32.AllocConsole();

            SortedList<string, string> sList1 = PopulateList1();
            SortedList<string, string> sList2 = PopulateList2();

            //get only values which are equal in both sortedLists:
            var difList = sList1.Intersect(sList2).Select(a => a.Value).ToList();
            foreach (var item in difList)
                Console.WriteLine(item);
            Console.ReadLine();
        }

        private SortedList<string, string> PopulateList1()
        {
            SortedList<string, string> sList1 = new SortedList<string, string>();
            for (int i = 0; i < 10; i++)
                sList1.Add("Key" + i.ToString(), "Value" + i.ToString());
            return sList1;
        }

        private SortedList<string, string> PopulateList2()
        {
            SortedList<string, string> sList1 = new SortedList<string, string>();
            for (int i = 6; i < 16; i++)
                sList1.Add("Key" + i.ToString(), "Value" + i.ToString());
            return sList1;
        }

Hope it helps,
Mitja

i had done 2 sortedlist.
in my first sortedList, the output for key and value are
1 : 4
2 : 4
3 : 4
1 2 : 3
1 3 : 3
2 3 : 3

in my second sortedlist , the output for key and value are
1 : 4
2 : 4
3 : 4

so now, how i combine this 2 sortedlist using intersect and hash. And i wan the output is like this:
1 -> 2
1 -> 3
2 -> 1
2 -> 3
1 2-> 3
1 3-> 2
2 3-> 1

hope anyone can help me..

I don't see how you get your last output from the two lists you provided.

If you want to combine lists use "Union" extension:

private void SortedLists()
        {
            SortedList<string, string> sList1 = PopulateList1();
            SortedList<string, string> sList2 = PopulateList2();

            //get only values which are equal in both 
            //var difList = sList1.Intersect(sList2).Select(a => a.Value).ToList();
            var combLists = sList1.Union(sList2).ToList();
            foreach (var item in combLists)
                Console.WriteLine(item);
            Console.ReadLine();

Even if I still dont get you. Your output is in way different format then sortedLists them self.
Are you sure you are not only wanted to combine lists? But you want to get "How many times the Keys are duplicating"? Am I right?

Mitja

BTW: I have checked your wanted output, and found that there is not any single connection. Where from did you get those nubmers:
1 -> 2
1 -> 3
2 -> 1
2 -> 3
1 2-> 3
1 3-> 2
2 3-> 1

Please, be clear in explaining these result of yours.
Mitja

lecturer ask me to use hash and intersect to compute the association rules. the result above is consider as association rules.

Did you read my post?
I CANNOT help you if you wont cooperate. I was asking what exactly is the output - where does it come from? I cannot create a code, if I dont know what to do.
But thats your problem.
When you will explain exactly how and what you want, you can expect from us some help. I hope I was clear enough I cannot do the code from the data you gave us.

Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.