Yow-Hann Lee - Software Happens

All things Computer Science, .NET & WWW

  Home  |   Contact  |   Syndication    |   Login
  129 Posts | 7 Stories | 30 Comments | 50 Trackbacks

News


Article Categories

Archives

Post Categories

About

Haven't been contributing to this series in a while, so here's a standard one to ease back into the flow...

Given any two large List<int>, what is the quickest way to find the mutual intersection of the two? Now take into consideration memory constraints?

posted on Friday, January 25, 2008 5:24 AM

Feedback

# re: Quick BrainSharpener (#11) 1/25/2008 1:19 PM Jonathan Starr
I am interested in your implementation, but I would use LINQ.

Here's something I whomped up quickly... The only thing that's kinda cool about it is that I leave the ordering details to LINQ, which will improve on its own over time, I suppose.

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.");
}

}

}


Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 2 and 5 and type the answer here: