Does OrderBy(item => false) affect list order? [duplicate] - c#

This question already has answers here:
Maintain subsequence order in OrderBy
(2 answers)
Closed 2 years ago.
Does OrderBy(item => false) affect array order?
I want to move certain element to the end of the array:
var numbers = new[] { 3, 7, 4, 1, 5 };
var result = numbers.OrderBy(item => item == 4).ToArray();
Does this solution have any side effects affecting the order of other elements (!=4)?

No this is fine, results with true (4) will be at the end of the list, your result will be 3, 7, 1, 5, 4

There is no side effects, as OrderBy operates on IEnumerable which doesn't have any methods to mutate collection

Related

How to create LINQ condition with an int Array? [duplicate]

This question already has answers here:
Intersect two arrays
(2 answers)
Closed 5 months ago.
I've got 2 arrays of Int, and I want to keep only elements from second array that contains the first array elements.
int [] first = new int[2] { 1, 2};
int [] second = new int[5] { 99, 1, 2, 97, 95};
I have tried something like below.
foreach(int x in first){
second.Where(s=>s==x);
}
But it doesn't help me because I need to compare both elements from first array
second.Where(s=>s==x[0] && s[1])
and if the int is bigger I need. Do you have any ideas how to get below code line?
second.Where(s=>s==x[0] && s== x[1] && ... && s==x[n])
var elements = second.Where(first.Contains);
Maybe materialize it with a .ToList() or ToArray() call.
If the first list is really large, you could think about a faster version than the .Contains method, but for your lists, it would be overkill.
var firstSet = first.ToHashSet();
var result = second.Where(x => firstSet.Contains(x)).ToArray();

Fast Sequence checking in a List<int> [duplicate]

This question already has answers here:
Is there a built-in method to compare collections?
(15 answers)
Functional way to check if array of numbers is sequential
(7 answers)
Closed 4 years ago.
I am looking for a faster and more accurate way to check a Sequence:
List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 … 41}
private bool IsSequential(List<int> sequence)
{
int S = sequence[0];
int T = sequence[sequence.Count- 1];
List<int> Possible = Enumerable.Range(S, T).ToList();
List<int> Except = sequence.Except(Possible).ToList();
if (Except.Count == 0)
return true;
else
return false;
}
My code returns 1 if the list is the same, I have some sort of count issue?
I wonder if there is a better way to check an integer sequence: 200, 201, 202... and so on.
Some Sequences may be out of sequence: 200, 210, 203, 204... I need to identify this issue.
Thanks
You can try like following using SequenceEqual.
List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
bool isInSequence = sequence.SequenceEqual(Enumerable.Range(sequence[0], sequence.Count()));

How to split an array to 2 arrays with odd and even indices respectively? [duplicate]

This question already has answers here:
Getting odd/even part of a sequence with LINQ
(8 answers)
Closed 6 years ago.
How to split an array to 2 arrays with odd and even indices respectively? For example
int[] a = new int[]{1, 3, 7, 8};
then get two arrays
a1: {1, 7}
a2: {3, 8}
Simple using the overload of Where than contains the index which:
Filters a sequence of values based on a predicate. Each element's
index is used in the logic of the predicate function.
int[] a = new int[] { 1, 3, 7, 8 };
int[] aEven = a.Where((x, i) => i % 2 == 0).ToArray();
int[] aOdd = a.Where((x, i) => i % 2 != 0).ToArray();

Deleting same elements in array C# [duplicate]

This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Closed 9 years ago.
If I have one array for example [1 4 3 7 4 9 5 1 5 6 3].
How to delete repetitive numbers and give in the output array like this [1 4 3 7 9 5 6]?
Distinct()
var distinctArray = myArray.Distinct().ToArray();
You can call your array in a HashSet<int> constructor. HashSet is a kind of optimized collection. It's constructor eliminates the non-unique elements.
Here an example in LINQPad;
var array = new[]{1, 4, 3, 7, 4, 9, 5, 1, 5, 6, 3};
HashSet<int> h = new HashSet<int>(array);
h.ToArray().Dump();
Here a result;
What about this :
int[] arr = { 1, 4, 3, 7, 4, 9, 5, 1, 5, 6, 3 };
foreach (int item in arr.Distinct())
{
Console.WriteLine(item);
}
and you can also assign in a array like this:
int[] unique = arr.Distinct().ToArray();
Couple of suggestions found searching:
1.)
int[] s = { 1, 2, 3, 3, 4};
int[] q = s.Distinct().ToArray();
2.) The easiest solution will be to simply sort the array (takes O(n log n) with standard implementation if you may use them. otherwise consider making an easy randomized quicksort (code is even on wikipedia)).
Afterwards scan it for one additional time. During that scan simple eliminate consecutive identical elements.
If you want to do it in O(n), you can also use a HashSet with elements you have already seen. Just iterate once over your array, for each element check if it is in your HashSet.
If it isn't in there, add it. If it is in there, remove it from the array.
Note, that this will take some additional memory and the hashing will have a constant factor that contributes to your runtime. Althought the time complexity is better, the practical runtime will only be onyl be faster once you exceed a certain array size
If you, for whatever reason, do not want to use Linq:
List<int> distinctList = new List<int>();
foreach (var num in numberList)
{
if (!distinctList.Contains(num))
{
distinctList.Add(num);
}
}

Is there an "AllIndexesOf" method? [duplicate]

This question already has answers here:
Getting a collection of index values using a LINQ query
(6 answers)
Closed 9 years ago.
Background:
I'm working on an evaluator (I know there's solutions available, but I need some features that I need to implement myself). I need to find all occurrences of open brackets in the evaluation. However, for that I need all the indexes of the brackets.
Question:
Is there something like an AllIndexesOf method that returns a int[], or IEnumerable<int>?
There is not but you can get all the indexes using the following LINQ query.
int number = 10;
int[] intArray = new[] { 1, 32, 10, 5, 65, 6, 10, 10 };
var allIndexes = intArray.Select((r,i)=> new {value = r, index = i})
.Where(r=> r.value == number)
.Select(r=> r.index);
allIndexes will contain 2,6 and 7
You also can use Enumerable.Range
var indexes = Enumerable.Range(0, list.Count)
.Where(index => list[index] == yourValue);

Categories