This question already has answers here:
Comparing two arrays in C#
(11 answers)
Closed 7 years ago.
i can't find same values in two integer arrays. Here is my code
int[] array1 = { 4, 5, 6, 7, 8, 9, 10 };
int[] array2 = { 1, 2, 3, 4, 5, 6 };
How to check same value?
I tried;
IEnumerable<int> k = (from d1 in array1
select d1).Intersect(from d2 in array2
select d2).ToList();
foreach (var item in k)
{
Console.WriteLine(item);
}
It is true, but i don't want to do this way.
Thank you.
Here is a 1 liner:
array1.Intersect(array2).ToList().ForEach(i => Console.WriteLine(i));
Related
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()));
Please let me know how can I sort a List which contains lists of integers.
List<List<int>> numberLists = new List<List<int>>();
numberLists.Add(new List<int>() { 6, 8, 9 });
numberLists.Add(new List<int>() { 2, 4, 7 });
numberLists.Add(new List<int>() { 4, 7, 8 });
numberLists.Add(new List<int>() { 2, 3, 9 });
How to sort the above List to have following result?
2, 3, 9
2, 4, 7
4, 7, 8
6, 8, 9
Thank you in advance!
You can do this
var results = numberLists.OrderBy(x => x[0])
.ThenBy(x => x[1])
.ThenBy(x => x[2]);
foreach (var result in results)
{
foreach (var subresult in result)
{
Console.Write(subresult + " ");
}
Console.WriteLine();
}
Output
2 3 9
2 4 7
4 7 8
6 8 9
Full Demo here
Additional Results
Enumerable.OrderBy Method (IEnumerable, Func)
Sorts the elements of a sequence in ascending order according to a
key.
Enumerable.ThenBy Method (IOrderedEnumerable, Func)
Performs a subsequent ordering of the elements in a sequence in
ascending order according to a key.
You can do it in a simple way base on your criteria
var sortedLists = numberLists.OrderBy(x => string.Join(",", x.ToArray())).ToList();
just convert it to string then compare as string
This question already has answers here:
Getting the "diff" between two arrays in C#?
(5 answers)
Closed 6 years ago.
How I can delete a numbers from Array “a” that contained in Array “b”?
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] b = {3, 9};
You cannot delete items from an array. What you can do is create another array that contains the items from a except the items in b and assign it to variable a like this:
a = a.Except(b).ToArray();
You can copy b into a list, and then delete elements from it.
List<int> bList = new List<int>();
bList.AddRange(b);
foreach (int check in a)
{
if (bList.Contains(check))
{
bList.Remove(check);
}
}
b = bList.ToArray();
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();
This question already has answers here:
Merging two arrays in .NET
(26 answers)
Closed 6 years ago.
I'm trying to make a console app with two double type arrays, but i have a problem.
How can i add two arrays into one, but without repeating same number (i have same number in two arrays)?
Thank you very much
Use Union from Linq:
double[] a = new double[] { 1, 2, 3, 4 };
double[] b = new double[] { 3, 4, 5, 6 };
double[] arr = a.Union(b).ToArray();
Variable arr will now contain 1, 2, 3, 4, 5, 6.