Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

Finding First Unique Number

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
24 Jul 2014CPOL1 min read 16.6K   7   6
Looking at ways to get the first unique number in a sequence

Introduction

Recently, I took a test to find the first unique number in an array. I did manage to produce the right answer, but it was computationally too expensive. This post is the result of looking into alternatives.

Original Solution

C#
var y = numberArray.GroupBy(z => z).Where(z => z.Count() == 1)
                                   .Select(z => z.Key).ToList();
 if (y.Count > 0) actual = y[0];

Alternatives

I asked for help on Stack Overflow as I wanted to see how other devs approached the solution and following are alternatives that where suggested:

  1. Change from y.Count to y.Any
  2. numberArray.GroupBy(g => g).Where(w => w.Count() == 1).Select(s => s.Key).FirstOrDefault();
  3. numberArray.ToLookup(x => x).First(x => x.Count() == 1).Key;
  4. numberArray.GroupBy(x => x).First(x => x.Count() == 1).Key;

In order to make head or tail of the speed difference, I decided to run some tests, the template for each test is.

C#
var sw = new System.Diagnostics.StopWatch2() { ShowStatsForEachLoop = false };
int actual = 0;
//////////////////////////////////////
sw.Restart();
sw.Start();
for (int i = 0; i < Iterations; i++)
{
   ///
     Find comparison here
   ///
    sw.RestartAndLog();
}
sw.Stop();

Test Results

It was surprising how big a difference switching to use .Any() made. The other surprise was the difference between .GroupBy() and .ToLookup()

If you want speed, then you should look at using traditional loops instead of LINQ.

All solutions were run 50 times with an array of 9 numbers, the last number being the unique number. The results below record the average number of CPU ticks for each test.

Ticks
   Min   Max     Avg  Result
    2  14502     584  -3     Original
    2    919      40  -3     if (y.Any()) actual = y[0];
    2   1423      60  -3     a.GroupBy(g => g).Where(w => w.Count() == 1)
                                            .Select(s => s.Key)
                                            .FirstOrDefault();
    2   1553      65  -3     a.ToLookup(i => i).First(i => i.Count() == 1).Key;
    2    317      15  -3     a.GroupBy(i => i).First(i => i.Count() == 1).Key;
    1     4        2  -4     Traditional loops

Source Code

The source code can be found on gitHub @ https://github.com/hungrydev/FirstUniqueNumberExporation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionJitter time? Pin
Matt T Heffron28-Jul-14 12:48
professionalMatt T Heffron28-Jul-14 12:48 
AnswerRe: Jitter time? Pin
Bruce Bennett28-Jul-14 23:09
Bruce Bennett28-Jul-14 23:09 
QuestionTraditional? Pin
irneb10-Jul-14 5:42
irneb10-Jul-14 5:42 
AnswerRe: Traditional? Pin
Bruce Bennett10-Jul-14 6:22
Bruce Bennett10-Jul-14 6:22 
GeneralRe: Traditional? Pin
irneb10-Jul-14 18:00
irneb10-Jul-14 18:00 
GeneralMy vote of 4 Pin
CatchExAs9-Jul-14 3:51
professionalCatchExAs9-Jul-14 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.