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);
Related
This question already has answers here:
How to unifiy two arrays in a dictionary?
(4 answers)
Closed 4 months ago.
I have two arrays (that are always going to have the same length). Something like
array1 = [ "a", "b", "c" ]
array2 = [ 1, 2, 3 ]
I'd like to know if there's a way to merge both arrays into a single JSON object in C# to something like
{
"a": 1,
"b": 2,
"c": 3
}
any help is appreciated.
It was that simple: Zip + ToDictionary before serialization.
using System.Text.Json;
using System.Text.Json.Serialization;
var array1 = new string[]{ "a", "b", "c" } ;
var array2 = new int[] { 1, 2, 3 };
var dict = array1
.Zip(array2, (a1,a2)=> (key:a1, value:a2))
.ToDictionary(k => k.key, v => v.value);
Console.WriteLine(JsonSerializer.Serialize(dict));
Zip takes two collections of the same size and creates a new one based on the result of a merge function (here, I used it to create tuples)
ToDictionary creates an enumerable to a dictionary structure.
Outputs:
{"a":1,"b":2,"c":3}
One note about this answer though: If you have two or more identical items in the first collection, this WILL throw an exception.
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();
This question already has answers here:
Can someone help me assign multiple string arrays into one 2d string array?
(3 answers)
Closed 2 years ago.
I have a 2d array group
float[,] group = new float [2,3];
My understanding is that, after this line of code I have 2 3-elements arrays in group.
{{3elements},{3elements}}
I also have member
float[] member = new float[3] {1,2,3};
I want to set the first array inside the 2d array "group" to match "member" so that I can have {{1,2,3},{}}
I tried group[0] = member;
But I got an error. What's the correct way of doing this?
You can try this
float[,] group = new float[2, 3];
float[] member = new float[3] { 1, 2, 3 };
for(int i = 0; i < member.Length; i++)
{
group[0, i] = member[i];
}
This question already has answers here:
Fill List<int> with default values? [duplicate]
(5 answers)
Closed 3 years ago.
I'm looking for the equivalent of Array.Fill but for List
Is it possible to fill all list items with certain value
Array.Fill(counters, max); // this works
listName.Fill(5); //something like this
would fill a list with 5's
I don't want to use a loop
Given that the list have some items
Enumerable.Repeat(TResult, Int32) Method
var fives = Enumerable.Repeat(5, 10).ToList(); // create list with ten fives
You need to loop all items for existing list anyway, just wrap the loop with an extension method.
public static void FillWith<T>(this List<T> list, T value)
{
for (var i = 0, i < list.Count, i++)
{
list[i] = value;
}
}
Usage
var list = new List<int> { 1, 2, 3, 4, 5 };
list.FillWith(42);
var output string.Join(",", list); // 42,42,42,42,42
This question already has answers here:
What is the performance of the Last() extension method for List<T>?
(5 answers)
Closed 5 years ago.
How effective is using Last() for arrays?
var array = new[] { ... };
var last = array.Last(); // or array[array.Length - 1]
In sources it only distinguish IList<T>, so is that true what Last() will enumerate a complete array to return last item? Funny stuff is msdn example without a single note.
Array does implement IList<T>:
var array = new int[] { 1, 2, 3 };
var list = (IList<int>)array;
So Last will not enumerate all array but instead will do array[array.Length - 1] (with a check that array is not empty of course - it which case it will throw).