This question already has answers here:
Simplest way to form a union of two lists
(5 answers)
Closed last year.
hi please help me
string [] s = new string []{1 , 2 , 3 };
string [] e = new string [] {1 , 2 ,4};
the output what I want are :
1 2 3 4
You are declaring a string[], however your values are int. You must either use string values or change the type of your array to int.
Union of IEnumerable is the function you're looking for and it does everything for you.
int [] s = new int [] {1, 2, 3};
int [] e = new int [] {1, 2, 4};
int[] union = s.Union(e).ToArray();
Related
First. I'm sorry if the question the question is badly formulated because I don't really understand the question myself.
Hello,
I'm currently helping a friend with his practice tasks about 2-Dimensional Arrays.
The following task is: Expand the array with the missing columns. Fill the column with two values each.
Now my question is how? Is it even possible to put 2 values in one?
For a better understanding, see this code:
static int[][] BSP = {
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3 } ,
new int[] { 1, 2, 3, 4, 5, 6 }
};
Now we should expand the array to the task I mentioned above. I understand the Task like this:
Instead of one value there should be 2.
Can you help me or did I misunderstood the question.
When you are going to expand (or shrink) try to use list List<T> instead of array T[]. We want to extend lines while keeping columns intact, so we can use List<T[]> - list of arrays:
static List<int[]> BSP = new List<int[]>() {
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3, 4, 5, 6 },
};
then you can just Add to extend (and RemoveAt to shrink):
// 4th row with 2 values: 12 and 21
BSP.Add(new int[] { 12, 21 });
This question already has answers here:
How to find a sub-list of two consecutive items in a list using Linq
(5 answers)
Closed 2 years ago.
With strings, you can search a string for a substring quite easily. But does .Net provide anything comparable to do this for more general collections, specifically for ordered data?
For instance to search [1,2,3,4,5,6,7,8,9] for the first occurrence of [4,5,6] would find a match but [6,5,4] would not be found? Perhaps as a member of Array or IEnumerable? It's an easy thing to roll but it seems likely it probably exists somewhere?
Let's suppose that you have the following arrays as you proposed:
var target = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var testA = new int[] { 4, 5, 6 };
var testB = new int[] { 6, 5, 4 };
Then you can search for a particular pattern in a given order by combining the Intersect and SequenceEqual methods:
Console.WriteLine(target.Intersect(testA).SequenceEqual(testA)); //true
Console.WriteLine(target.Intersect(testB).SequenceEqual(testB)); //false
This question already has answers here:
Compare two lists to search common items
(2 answers)
Closed 6 years ago.
I have two lists of integers, I want to compare them and save the same values in a new list.
So if I have:
List<int> list1 = new List<int>()
{
1,
2,
3
};
List<int> list2 = new List<int>()
{
2,
3,
4
};
I would want a new List<int> List3 that would contain numbers 2 and 3.
You can use the Linq Intersect methods which gives the common part:
List<int> a = new List<int>();
List<int> b = new List<int>();
var common = a.Intersect(b);
This question already has answers here:
Convert 2 dimensional array
(4 answers)
Closed 7 years ago.
Is there an elegant way to flatten a 2D array in C# (using Linq or not)?
E.g. suppose
var my2dArray = new int[][] {
new int[] {1,2,3},
new int[] {4,5,6}
};
I want to call something like
my2dArray.flatten()
which would yield
{1,2,3,4,5,6}
Any ideas?
You can use SelectMany
var flat = my2dArray.SelectMany(a => a).ToArray();
This will work with a jagged array like in your example, but not with a 2D array like
var my2dArray = new [,] { { 1, 2, 3 }, { 1, 2, 3 } };
But in that case you can iterate the values like this
foreach(var item in my2dArray)
Console.WriteLine(item);
As I understand it, C# has a syntax for writing arrays as such: { 1, 2, 3 }. Why is this invalid:
x = { 1, 2, 3 }.GetLength(0);
while this is valid?
int[] numbers = { 1, 2, 3 };
x = numbers.GetLength(0);
Isn't the datatype of the expression { 1, 2, 3 } the same as numbers?
Arrays are allowed anywhere - but you can only use that particular syntax (which is called an array initializer for creating them as part of a variable declaration - or as part of a larger expression called an array creation expression.
You can still create them though:
x = new int[] { 1, 2, 3 }.GetLength(0);
So within that, new int[] { 1, 2, 3 } is the array creation expression, and the { 1, 2, 3 } part is the array initializer.
Array creation expressions are described in section 7.6.10.4 of the C# 5 spec, and array initializers are described in section 12.6.
The syntax you refer to is an object collection initializer. It is useful when initializing an instance of different types. It does not, in itself, create an instance of a given type.
For instance, you can use it to declare arrays:
int[] nums = new int[] { 1, 2, 3 };
Lists:
List<int> nums = new List<int> { 1, 2, 3 };
Dictionary:
Dictionary<string, int> pairs = { { "One", 1 }, { "Two", 2 }, { "Three", 3 } };
You can still inline things to achieve your initial intention with a little more code:
new[] { 1, 2, 3 }.GetLength(0);
x = new[] { 1, 2, 3 }.GetLength(0); will get you what you want since {1, 2, 3} isn't on its own an array, but rather an array initializer. And GetLength() works with the former but not the latter.