score:16
you were nearly there before - but you were comparing with characters instead of integers.
if you want to check whether everything is strictly positive, use:
bool allpositive = array.all(x => x > 0);
if you actually want to check they're all non-negative (i.e. 0 is acceptable) use:
bool allnonnegative = array.all(x => x >= 0);
you should definitely consider what you want to do with 0, as your problem statement is actually contradictory. ("all positive" and "no negative" aren't the same thing.)
note that just like any
, all
will quit as soon as it knows the result - so if the first value is negative, it won't need to look through the rest.
score:1
below is a c# solution to your problem (it should be rather efficient performance-wise):
int[] array = new int[] { 1, 5, 11,5 };
bool _allpositive=true;
for (int i=0;i<array.length;i++){
if(array[i]<0 {_allpositive=false; break;}
}
bool _allpositive
indicates the result (you can use string var if you prefer). essentially, the same syntax can apply to java-like languages, including scripting.
score:1
string result = array.any(x => x < 0)?"wrong":"positive";
score:3
you almost there. but in your condition, you comparing characters instead of integers which is wrong in your situation.
use enumerable.all
like;
determines whether all elements of a sequence satisfy a condition.
int[] array = new int[] { 1, 5, 11, 5 };
bool allpositive = array.all(x => x > 0);
console.writeline(allpositive);
here is a demo
.
remember 0
is not positive.
score:6
use enumerable.any like:
if(array.any(r => r < 0))
{
//negative number found
}
else
{
//all numbers are positive
}
or you can use enumerable.all like:
if(array.all(r => r > 0))
{
//all numbers are positive
}
else
{
//negative number found
}
Source: stackoverflow.com
Related Query
- How to check if all the elements in an array are positive integers?
- How to check if all values in an array are equal
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to check if all of the elements in a list return true for a property using Linq?
- How to check if all strings in a string array are all digits?
- How to check if all elements of a list are different
- How do I get all the elements and if they are assigned?
- How to check if all items in an array of strings are key in a NameValueCollection?
- Filter all the Dictionary elements which are matching with the input string array only or any number
- Get all elements but the first from an array
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- How to make a linq Sum return null if the summed values are all null
- How to get the top 3 elements in an int array using LINQ?
- How to join together all the elements in an IEnumerable of IEnumerables?
- How to get all elements except the n'th element in a List using Linq
- C#: how do you check lists are the same size & same elements?
- c# Check if all strings in list are the same
- How do I use Linq to find the elements of a list that are not present in another list?
- How can I loop Through all the elements in .select statement in Linq
- How to include all elements of an array in a SQL query?
- How to compare the elements of an char array using LINQ?
- How to get the list elements that are not into another list C#
- Linq Query to check if the records are all the same
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- LINQ To XML - How to query for all the elements in an inner element?
- How to use CollectionAssert (and Linq?) to find all the values for one property, are the same?
- Expression to find all objects that contain all elements in an array of integers
- C# How to make recursive function to return the nth most common integer in an array of integers
- How to repeat the array elements with specific sequence according to some number?
- How to get specific items from List<object> when all the object are different types of EventArgs
More Query from same tag
- Best way to convert a huge collection of key value pairs to C# object
- Fluent Assertions between two collection class
- Calculate Average Age from a list of dates using linq
- How do I write this in Linq to reduce code?
- Reading Xml file using LINQ in C#
- The use of where in Linq
- Selecting values from List
- How-to ensure multiple collections have the same item count using LINQ
- Modify existing Element in XMLfile
- List does not contain a definition for 'ConvertAll' and no extension method 'ConvertAll' accepting a first argument of type 'List< >'
- What is the most efficient way to join two lists in C# on a common field?
- Getting Only The Top Row From Each Group
- Flatten Dictionary with a List and return Custom IEnumerable
- How can I filter a list to exclude elements where the InnerText property does not contain "See {"
- Creating a method that queries a database using linq
- how to write Linq Query for following ouput
- Anonymous Types in a signature
- How can you do custom sorting in LINQ with null always on the end?
- How to detect that a datatable is a part of another datatable
- How Linq's GroupBy method has a deferred execution?
- Linq to XML GroupBy then OrderBy
- How can I get two different aggregates in a single LINQ?
- update a property value during linq to sql select (involves join)
- Handling large SQL queries with LINQ
- Linq - How Do I select a new object to contain a list of other objects?
- Use an additional condition/parameter for Enumerable.GroupJoin
- Where clause in complex linq query giving the following error "A query body must end with a select clause or a group clause linq"
- LINQ Query to Convert string to datetime
- Converting ORDER BY with multiple case statements
- LINQ Find String Duplicates in List