Geeks With Blogs
Jonathan Starr's Blog Thoughts on C#, Ajax, WCF, LINQ, Agile et al.

I have been pretty excited about LINQ, because it seems to do all of the optimization for me for free (kinda like T-SQL). 

Yow Han-Lee asks in his blog, Brainteaser #11, Given any two large List, what is the quickest way to find the mutual intersection of the two? Now take into consideration memory constraints?

 Well, the answer that only takes a minute or two for me is the following...

Comments welcome!

Jonathan Starr

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace orderlisttest

    internal class Number
        private int _Num1;
        public int Num1
            get { return _Num1; }
            set { _Num1 = value; }

        public Number(int theNumber)
            this._Num1 = theNumber;

    public class start

        public static void Main(string[] args)
            var bigList1 = new List<Number>();
            var bigList2 = new List<Number>();

            for (int counter = 0; counter < 100000; counter++)
                Random randomGenerator = new Random();
                int number = randomGenerator.Next(10000000);
                bigList1.Add(new Number(number));

                int number2 = randomGenerator.Next(10000000);
                bigList2.Add(new Number(number2));

            var Found = from o1 in bigList1
                        join o2 in bigList2 on o1.Num1 equals o2.Num1
                        select new { o1.Num1 };

            Console.WriteLine(Found.Count().ToString() + " items were found in common.");


Posted on Friday, January 25, 2008 3:23 PM C# , Software Design , Software Development | Back to top

Comments on this post: Response to Brainteaser #11?

# re: Response to Brainteaser #11?
Requesting Gravatar...
It will be 0(Zero)
Left by geek on Feb 24, 2011 7:02 AM

Your comment:
 (will show your gravatar)

Copyright © Jonathan Starr | Powered by: