score:17
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))));
Source: stackoverflow.com
Related Query
- Is it possible to express this code in LINQ?
- How to handle nulls in this LINQ Code using a possible null List?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Does this LINQ code perform multiple lookups on the original data?
- Is it possible to refactor this nHibernate Linq query?
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- multiple orderby in this linq code
- How can I combine this code into one or two LINQ queries?
- How can I further simplify this piece of LINQ code
- Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)
- How to render this map-reduce javascript code to an equivalent LINQ Select-Aggregate?
- LINQ approach to this code
- creating Linq to sqlite dbml from DbLinq source code
- How do i convert this linq code to inline sql
- Is It Possible to do this T-SQL Query with LINQ (to Entities)
- why does this linq code get exponentially slower when applying First() to projection?
- How to convert this recursive code to Linq
- How to debug this or possible causes? System.NullReferenceException was unhandled by user code on Moq.Verify
- Linq to Xml, is it possible to improve this query?
- Using Linq to build a graph class; can you make this code look better?
- How to write this code using the Linq Extension Method-Syntax?
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- Is it possible to do this as a single efficient LINQ query?
- How can I refactor this code for LINQ filtering?
- Why this Linq code always throws System.StackOverflowException?
- Make access possible to dynamic table LINQ EF6 Code First
- My code is very inefficient for this simple Linq usage
- Is it possible to use Linq to replace this Foreach loop
- How I change this code to be in linq style
More Query from same tag
- How to simultaneously sort 2 lists using LINQ?
- get value Session
- Linq Query For AutoComplete Match With Multiple Columns C#
- Benefits of manual lookup over joining in LINQ?
- System.InvalidCastException for DataModel Creation using Sqlite as Database
- Get the last entry in database of two different dates and calculate the difference
- What are some clever uses of LINQ?
- Linq to Entities query really slow. What can I do?
- Most efficient collection for storing data from LINQ to Entities?
- Condense Linq logic into one query
- How can I join multiple tables including a join-table (auto-generated by Entity Framework) using LINQ
- 'string' does not contain a definition for 'Contains'
- Search Specific Duplicated keys in List C#
- How do I copy projected results into another variable in c#?
- Convert a List of Anonymous type to a list of a specific Class type
- LinQ Remove products based on Status
- IEnumerable<SelectListItem> in C# mvc
- Linq retrieving items that have changed between sets and items not in sets
- What is the purpose of using Select(x => x) in a Batch method?
- Vb.net Linq order by special
- Is there a Lambda Else condition?
- Exclude columns on Update Linq to entity
- Advanced LINQ Grouping and Projection Query for Hierarchical Data (EF 4.0 + LINQ + ASP.NET MVC + HighCharts)
- Convert observablecollection of paths into collection of filenames
- How can I do a "one hit" Linq query with nullable values?
- How to check value of item is not equal to zero before adding in a list?
- Update single table from a Model with referential constraint without modifying referenced table
- Given XElement - Find node text by node name
- select in linq and access to element
- How to tell "OfType()" to ignore inherited classes?