2018年4月10日 星期二

[Logic] Find the unique number

題目 :
There is an array with some numbers. All numbers are equal except for one. Try to find it!

findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55

-----

my ans :
 public static int GetUnique(IEnumerable<int> numbers)
  {
     var result = numbers.GroupBy(
      c => c,
      (key,g) => new { a = key, count = g.Count()})
      .Where(c => c.count == 1)
      .Select(c => c.a).Single();

     return result;
  }

沒有留言:

張貼留言