C# Linq, Find highest number from list using linq [duplicate] - c#

This question already has answers here:
Maximum integer value find in list<int>
(7 answers)
Closed 8 years ago.
I have below list, how do i find the highest or max number.
List<int> numbers = new List<int>();
numbers.Add(2);
numbers.Add(5);
numbers.Add(7);
numbers.Add(10);
Please help!

you just need to use Max method of list.
i.e :
var result = numbers.Max();

Use Max:-
int highestNum = numbers.Max();

Related

Random Number Generator with no repeats [duplicate]

This question already has answers here:
Random number generator with no duplicates
(12 answers)
Generating random numbers without repeating.C# [duplicate]
(11 answers)
Closed 1 year ago.
I am trying to work out how to make a random number generator that outputs 4 integers going from 0 to 9 without any repeats.
Would like some help please
i have just started coding in c# but cant find any answers to my issue
You need to remove the int from the checking if it has already repeated.
so:
if (val1 == val2)
{
val2 = rnd.Next(1,11);
}
and not:
if (val1 == val2)
{
int val2 = rnd.Next(1,11);
}
The latter will declare a new variable "val2" which exists only inside the scope of the "if" block, instead of updating the existing "val2" variable as you intended. This is called "shadowing".
For "quick and dirty" solution you can use LINQ. For example something like this will randomly select 4 numbers in range from 0 to 9:
var random = new Random();
var numbers = Enumerable.Range(0, 10)
.OrderBy(_ => random.Next())
.Take(4)
.ToList();
In case you need something more prescise and faster then you can implement Fisher–Yates shuffle for example.

get all elements that does not exist in a c# list [duplicate]

This question already has answers here:
Check for missing number in sequence
(14 answers)
Closed 4 years ago.
I have a list of numbers:
List<int> lstActive = new List<int>{1,6,7,8,10};
I want to get the numbers that does not exit in the above list and less than 10
e.g.
private List<int> GetInactive(List<int> lstActive, int MaxValue)
{
//To Do
}
Then:
List<int> lstInactive = GetInactive(lstActive, 10)
the result should be:
{2,3,4,5,9}
How can I do this ?
Enumerable.Range(0, maxValue).Where(n => !lstActive.Contains(n))
If perf is an issue, make a hashset:
var hs = new HashSet<int>(lstActive);
Enumerable.Range(0, maxValue).Where(n => !hs.Contains(n))
Try This:
List<int> lstActive = new List<int>(new int[]{1,6,7,8,10});
Enumerable.Range(1, 10).ToList().Except(lstActive).Dump();
https://dotnetfiddle.net/hyiAhs

C# - Field values of an array picking up values of a loop [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
How do I clone a range of array elements to a new array?
(26 answers)
Closed 5 years ago.
I'm training C# on a simple card game. I have methods that shuffle and deals the cards. I have a random deck that is well generated.
Is it possible to set an array for the player1 cards, picking up the first ten values of the array.
Here is a part of my code :
currentCard = 0;
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}
I want to pick up for example ten first values of
deck[currentCard++]
Any suggestions will be appreciated, thanks for your help !
You mean you want to pull the first 10 enties into another array? Something like;
var player1Cards = deck.Take(10);
or
List<int> player1Cards = new List<int>();
for (int i = 0; i < 10; i++){
player1Cards.Add(deck[i]);
}

Get the index of an element in a List that satisfies a condition [duplicate]

This question already has answers here:
How do I get the index of the highest value in an array using LINQ?
(10 answers)
Closed 7 years ago.
Say I have a Point:
public class Point
{
double X, Y;
}
I want to get the index of the element inside List<Point> that satisfy a condition, for example, with the maximum Point.X value inside List<Point>.
How would I do that with a LINQ expression?
You can do using this Select() overlaod which takes index of item as well:
var result = Points.Select((Point,Index)=> new { Index,Point})
.OrderByDescending(x=>x.Point.X).First().Index;

How to replace value in list at same collection location [duplicate]

This question already has answers here:
How to replace list item in best way
(12 answers)
Closed 5 years ago.
How do I replace a value in a collection list at the same location?
0 = cat
1 = dog
2 = bird
replace 2 with snail?
Do you mean:
yourCollection[2] = "Snail";
In a bigger List<T> collection, you would like to find index to replace...with:
var i = animals.FindIndex(x => x == "Dog");
animals[i] = "Snail";
List<string> animals = new List<string>();
animals.Add("cat");
animals.Add("dog");
animals.Add("bird");
animals.Add("fish");
animals.RemoveAt(2);
animals.Insert(2, "snail");

Categories

Resources