I need one liner (or close to it) that verifies that given array of 9 elements doesn't contain repeating numbers 1,2,3,...,9. Repeating zeroes do not count (they represent empty cells).
The best I have came out so far is:
var a = new int[9] {1,2,3,4,5,6,7,8,9};
var itIsOk = a.Join(a, i => i, j => j, (x, y) => x)
.GroupBy(y => y).Where(g => g.Key > 0 && g.Count() > 1).Count() == 0;
If you don't want to solve my problems :), could you at least tell if the above algorithm works correctly?
And, yes, a have read this one.
For brevity if not performance, how about var itIsOk = a.Sum() == a.Distinct().Sum();
!a.GroupBy(i => i).Any(gp => gp.Key != 0 && gp.Count() > 1)
Why do you want a convoluted line of Linq code, rather than wrapping up an efficient implementation of the test in an extension method and calling that?
One implementation of NoNonZeroRepeats could be to use the 9 lowest bits of a short to indicate presence of a value in the array, giving an O(length(a)) test with no dynamic memory use. Linq is cute, but unless you're only using it for aesthetic reasons (you don't specifically say that you're writing a sudoku solver using only Linq as an exercise) it seems to be just adding complexity here.
I usually frown on solutions that involve captured variables, but I had an urge to write this:
The following is simple and fast.
This is about 50-250 times faster than a LINQ solution (depending on how early the duplicate is found):