We are trying to concatenate two arrays [duplicate] - c#

This question already has answers here:
What is the use of Enumerable.Zip extension method in Linq?
(9 answers)
Closed 4 years ago.
We are trying to concatenate two arrays, tried the concatenate, append to list join array without success. Is there an easy function to turn two lists {a,b,c} and {1,2,3} into {a1,b2,c3}? The arrays are multiples within CDT's and we're combining a text array with an integer array.

Try Linq Zip() which
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
string[] array1 = { "a", "b", "c" };
string[] array2 = { "1", "2", "3" };
string[] result = array1.Zip(array2, (x, y) => x + y).ToArray();
in this case it concartenates two strings (x, y) => x + y

Here I have assumed that both arrats have the same length:
List<string> result;
for(int i=0;i<array1.Length;i++)
result.Add(${array2[i]}{array1[i]});

Related

Does Array provide a way to search for index of an element with arbitrary logic [duplicate]

This question already has answers here:
Find index of a value in an array
(8 answers)
Getting the index of a particular item in array
(5 answers)
Closed 3 years ago.
Is there any lambda-based approach which will allow one to find the index of (the first) occurrence of an element in an array based on custom logic?
I know I can find the element itself using Array.First but can I find the index of that element without manually iterating the array?
You could use Array.IndexOf(). Example:
var array = new string[] { "one", "two", "three" };
var index = Array.IndexOf(array, "two");
// index = 1
You can use Array.IndexOf() that "Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array." :
var foo = new int [] { 10, 42, 42, 51, 42, 100 };
Console.WriteLine(Array.IndexOf(foo, 42)); // output is 1
Although this will iterate under the hood, you don't have to manually [iterate] the array
You can do this:
var s = new string[] {"foo", "bar"};
var index = Array.FindIndex(s, s1 => s1 == "foo"); // returns 0
NOTE: it will return -1 if the element was not found
There's the Array.IndexOf method.
The following code:
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
Console.WriteLine(Array.IndexOf(strings, "dog"));
Will output 8.
MSDN: https://learn.microsoft.com/en-us/dotnet/api/system.array.indexof

How to return two arrays using one WHERE statement

Is it possible to combine these two statements into one which returns "contains" and "not contains" results?
string[] words = { "one", "two", "three", "four" };
int[] numbers = { 4, 5, 6 };
string[] contains =
(from w in words
where numbers.Contains(w.Length)
select w).ToArray();
string[] notContains =
(from w in words
where !numbers.Contains(w.Length)
select w).ToArray();
You can do:
var groups = words.GroupBy(w => numbers.Contains(w.Length));
This will return at most two groups: one with the key false and one with the key true.
Edit
As vc 74 pointed out in the comments, numbers.Contains is an O(n) operation when numbers is an array. Converting it to a HashSet instead will make this a constant time operation, which is asymptotically much faster.
So, here's the updated code:
var numberHS = new HashSet<int>(numbers);
var groups = words.GroupBy(w => numberHS.Contains(w.Length));
You can also use ToLookup:
var containsLengthLookup = words.ToLookup(w => numbers.Contains(w.Length));
string[] contains = containsLengthLookup[true].ToArray();
string[] notContains = containsLengthLookup[false].ToArray();
If one of both is empty (or the source array is empty) you get an empty string[].
There's one difference to GroupBy, the lookup is cached. So it's more efficient if you use it multiple times, but the information is just a snapshot. If you modify words or numbers this isn't reflected in the lookup.

C# Iterate through two arrays or List and put matches in a third array or list [duplicate]

This question already has answers here:
Compare List and return matches in c#
(3 answers)
Closed 6 years ago.
I have two arrays
List<int> a
List<int> b
List<int> matches
And I need to put all matches in a third (match) array so that I can print that out...
I can print out both a and b like so.
a.Sort();
label1.Text = "";
foreach (int x in a)
label1.Text += x + " , ";
a.Clear();
And so on for "b"
but how to compare the two and only take the matching integers, put them in "matching" array and print them out the same way?
You could use a linq query to get values that are in both lists...
List<int> a = new List<int> {1,2,3};
List<int> b = new List<int> {2,4,6,3};
var matches = a.Intersect(b);
// Create comma-separated string of matching values...
string output = string.Join(",", matches);

I want count uniques along with value in array? [duplicate]

This question already has answers here:
How to Count Duplicates in List with LINQ
(7 answers)
Closed 9 years ago.
I have 6 numbers in array .
string[] list = { "1", "1", "2","2","1","3" };
I want result like this. please help.
"1" = 3
"2" = 2
"3" = 1
var itemCounts = list.GroupBy(l => l)
.Select(g => new { key = g.Key, count = g.Count()});
Assuming your numbers in SearchArray >0. Here is an alternative approach
You can also write a function
1) find Max - One Loop
for( int i=0;i<searchArray.length;i++){
if (searchArray[i]>max) max=searchArray[i];
}
2) Initialize an Array[Max+1]= 0
3) Loop thru each item and increment the size in Array
for( int i=0;i<searchArray.length;i++){
Array[searchArray[i]]++;
}

LINQ concatenating elements in two string arrays [duplicate]

This question already has answers here:
How can combine two enumerations with a custom function?
(4 answers)
Closed 9 years ago.
I have two string arrays
Array1 = {"Paul","John","Mary"}
Array2 = {"12","13","15"}
I would like to know whether it is possible to join these arrays so that the resultant arrays have something like
{"Paul12","John13","Mary15"}
var Array3 = Array1.Zip(Array2, (a, b) => a + b).ToList();
You can use a Zip.
var array1 = new[] {"Paul", "John", "Mary"};
var array2 = new[] {"12", "13", "15"};
var result = array1.Zip(array2, (a1, a2) => String.Concat(a1, a2)).ToArray();

Categories

Resources