Odd Man Out – March 2010

April 25th, 2010 by Daniel Herman Leave a reply »

Google Code Jam – Odd Man Out

The problem:
This problem is pretty easy. Basically, we have N number of integers with one of them being unique. It’s easy enough to simply search through a list of the provided integers until you run across one that has only 1 match in the list – that’s your answer.

And yes, I know I can break the loop after I’ve found the lone integer – since this is so quick running and it’s nothing important, I didn’t bother going back to fix that.

Here’s the C# code that I used to solve the problem.

 C# |  copy code |? 
01
using System;
02
using System.IO;
03
using System.Collections.Generic;
04
 
05
namespace odd_man_out
06
{
07
	class Program
08
	{
09
		public static void Main(string[] args)
10
		{
11
			string path = args[0];
12
 
13
			StreamReader reader = new StreamReader(path);
14
			StreamWriter writer = new StreamWriter(path + ".out", true);
15
 
16
			int numTestCases = int.Parse(reader.ReadLine());
17
			int currNum = 0;
18
 
19
			List<string> guests = null;
20
			int numGuests = 0;
21
 
22
			while (!reader.EndOfStream)
23
			{
24
				currNum++;
25
				guests = new List<string>();
26
				numGuests = Int32.Parse(reader.ReadLine());
27
				string input = reader.ReadLine();
28
 
29
				guests.AddRange(input.Split(new char[] { ' ' }));
30
 
31
				writer.Write("Case #" + currNum + ": ");
32
				foreach (string guest in guests)
33
				{
34
					if (guests.FindAll(
35
						delegate(string s)
36
						{
37
							return s == guest;
38
						}).Count == 1)
39
					{
40
					writer.WriteLine(guest);
41
					writer.Flush();
42
					}
43
				}
44
			}
45
		}
46
	}
47
}

Advertisement

Leave a Reply