Posts
70
Comments
66
Trackbacks
0
Response to Brainteaser #11?

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 Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 4 and 3 and type the answer here:
News
Jonathan Starr is a developer in Saint Louis, MO. He holds an MBA in Finance from Columbia Business School and earned his MCSD from Microsoft.


All statements in this blog are personal opinions and do not reflect the opinions of his employer.





Related Sites
Join My Community at MyBloglog!

Tag Cloud