Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Finding first unique number.

, 24 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Looking at ways to get the first unique number in a sequence.


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

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


I asked for help on Stack Overflow as I want to see how other devs approached the solution and the following are alternatives 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.

    var sw = new System.Diagnostics.StopWatch2() { ShowStatsForEachLoop = false };
    int actual = 0;
    for (int i = 0; i < Iterations; i++)
         Find comparison here

Test Results

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

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

All solutions where 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.

   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)
    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     Traditonal loops

Source Code

The source code can be found on gitHub @


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


About the Author

Bruce Bennett
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionJitter time? PinprofessionalMatt T Heffron28-Jul-14 13:48 
AnswerRe: Jitter time? PinmemberBruce Bennett29-Jul-14 0:09 
QuestionTraditional? Pinmemberirneb10-Jul-14 6:42 
AnswerRe: Traditional? PinmemberBruce Bennett10-Jul-14 7:22 
GeneralRe: Traditional? Pinmemberirneb10-Jul-14 19:00 
GeneralMy vote of 4 PinprofessionalCatchExAs9-Jul-14 4:51 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150224.1 | Last Updated 24 Jul 2014
Article Copyright 2014 by Bruce Bennett
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid