score:17

Accepted answer
if (ar.sequenceequal(ar.orderby(x => x)))
{
    if (ar.distinct().count() == ar.length)
        return 1;
    else
        return 2;
}
else 
{
    return 0;
}

score:0

untested.

ienumerable<int> signs = 
  from i in enumerable.range(0, ar.length).skip(1)
  select ar[i-1].compareto(ar[i]);

int result =
  signs.any(sign => sign < 0) ? 0 :
  signs.all(sign => 0 < sign) ? 1 :
  2;

also untested:

int minsign = !ar.skip(1).any() ? 1 :
(
  from i in enumerable.range(0, ar.length).skip(1)
  select ar[i-1].compareto(ar[i])
).takewhile(x => 0 <= x).min();

int result =
  minsign < 0 ? 0 :
  0 < minsign ? 1 :
  2;

score:3

as a note, your expressed non-linq logic has a flaw.

if (next < prev) 
    sortstatus = 0; 
if (next == prev) 
    sortstatus = 2; 

your rule says that the array must be sorted ascending but have duplicates in order to get an output of 2. however, your logic will return 2 for { 1, 9, 7, 7 }.

another way to write your code might be the following. (this is not using linq, but this is too long to post as a comment to your question.)

static int evaluatearray(int[] array)
{
    int? lastitem = null;
    bool match = false;
    foreach (int item in array)
    {
        if (item < lastitem)
            return 0;
        else if (item == lastitem)
            match = true;

        lastitem = item;
    }

    if (match)
        return 2;

    return 1;
}

in this method, we will early-return as soon as we have an item less than the previous item. otherwise, we will set a boolean if we come across a matching value. at the end of the loop, we know the array is sorted ascending. the only thing left is check if there was a match.

score:5

a pure linq alternative ... (for academic interest only (but probably still faster than the accepted answer!)

var input = new int[] { 1, 2, 3, 4, 5 };

var output = input.zip(input.skip(1), (a, b) => new {a=a, b=b})
                .aggregate(1, (status, x) => status == 0 ? 0 : ((x.a > x.b ? 0 : (x.a == x.b ? 2 : status))));

Related Query

More Query from same tag