i have a question how to concat or union 3 arrays in one line of the code?
I've got Visual Studio 2015 and it looks like
int[] array1 = {1 ,2 ,3 ,-5 ,2 ,0 };
int[] array2 = {1 ,2 ,3 ,-1 ,5 ,0 };
int[] array3 = {1 ,2 ,3 ,-6 ,2 ,9 };
and i wanna on button click to have like:
Console.WriteLine(array1.Union(array2.Union(array3)).Where((x) => x >=0).Count)
Dunno rly how to Union 3 array in single line
The problem with your code is that after the Where clause the Count is a function and not a property.
In addition a neater way will be to chain the Union instead. Also You can place the predicate in the Count instead:
Console.WriteLine(array1.Union(array2).Union(array3).Count(x => x >= 0));
To print only positive values of all arrays use string.Join:
Console.WriteLine(string.Join(",", array1.Union(array2).Union(array3).Where(x => x >= 0)));
//Prints: 1, 2, 3, 0, 5, 9
For a Union you just need
array1.Union(array2).Union(array3)
and then you can write and/or count the resulting IEnumerable. A Union disregards duplicates: if you want to preserve all the values from all 3 arrays (and continuing arrays instead of some list/collection type) you could do:
var concatenated = new int[array1.Length + array2.Length + array3.Length];
array1.CopyTo(concatenated, 0);
array2.CopyTo(concatenated, array1.Length);
array3.CopyTo(concatenated, array1.Length + array2.Length);
The following will concatenate the arrays by calling Concat.
Then remove the replicated numbers by calling Distinct.
Lastly filter out the numbers by calling Where with the predicate greater or equal to zero.
var arr1 = new[] {1, 2, 3, -5, 2, 0};
var arr2 = new[] {1, 2, 3, -1, 5, 0};
var arr3 = new[] {1, 2, 3, -6, 2, 9};
var result = arr1
.Concat(arr2)
.Concat(arr3)
.Distinct()
.Where(i => i >= 0)
.ToArray();
Can be done in one line of code.
Use linq SelectMany to flatten arrays or lists.
// Set up array of arrays sample.
var a1 = new string[] { "z","x","d" };
var a2 = new string[] { "a","e","i" };
var a3 = new string[] { "q","m","w" };
var arrays = new string[][] { a1, a2, a3 };
var r = arrays.SelectMany(s => s);
Generic concatenation of multiple arrays.
//
//
///<summary>Create a new array as concatenation of all given arrays.</summary>
public static T[] Concatenate<T>( params T[][] args ) {
if ( null == args ) return null;
// Get argument lengths
var count = args.Length;
if ( 0 == count ) return null;
var lengths = new int[ count ];
// Compute all and total lengths
var total = 0;
for ( int i = 0; i < count; i++ ) {
lengths[ i ] = null == args[ i ] ? 0 : args[ i ].Length;
total += lengths[ i ];
}
if ( 1 > total ) return null;
// Create target array
T[] a = new T[ total ];
// Copy all together
total = 0;
for ( int i = 0; i < count; i++ ) {
if ( null != args[ i ] ) {
args[ i ].CopyTo( a, total );
}
total += lengths[ i ];
}
return a;
}
Related
Given two Lists of integer arrays of the form:
genData = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}};
orgData = {{1,2,3,4}, {1,2,3,4}, {2,4,6,8}, {1,2,3,4}};
I'd like to determine if the sum of two subarrays at the same index in both lists don't match. If the sums match, do nothing. If the sums don't match, convert every integer in both subarrays into a 0.
For example, in the two lists above the subarrays at index 2 have a non-matching sum (10 vs 20). I'd like to convert the lists to
genData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
I'm trying to first create a list if sums by trying
var genDataSum = genDataList.ForEach(x => x.Sum());
But obviously, that's throwing up errors..."Cannot assign void to an implicitly typed value".
Any help or guidance will be greatly appreciated.
You need to use select to get the sum. list.foreach works like normal for loop.
List<int[]> genData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 }
};
List<int[]> orgData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 2, 4, 6, 8 },
new int[] { 1, 2, 3, 4 }
};
var sumsGenData = genData.Select(a => a.Sum()).ToList();
var sumsOrgData = orgData.Select(a => a.Sum()).ToList();
for (int i = 0; i < sumsGenData.Count; i++)
{
if (sumsGenData[i] != sumsOrgData[i])
{
orgData[i] = new int[] { 0, 0, 0, 0 };
}
}
ForEach doesn't return anything. Use Select.
var orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
var sums = orgData.Select( a => a.Sum() );
I want to find the index of sublist in list route which contains certain value(i), but I don't want to make a class of it.
Here is my code:
var route = new List<List<int>>();
for (int i = 0; i<DC1; ++i)
{
for (int j = 0; j<DC1; ++j)
{
if (routeopt.x[0, j] == 1)
{
List<int> subroute = new List<int>();
if (routeopt.x[i, j] == 1)
{
subroute.Add(j);
route.Add(subroute);
}
}
}
if(i == 0) continue;
for (int j = 1; j<DC1;j++ )
{
if (routeopt.x[i, j] == 1)
route[route.IndexOf(i)].Add ( j);
}
}
foreach (var subroute in route)
{
Console.Write("subroute: ");
foreach (int s in subroute)
Console.Write(s + " ");
Console.WriteLine();
}
For example, based on this code:
for (int j = 1; j < DC1;j++ )
{
if (routeopt.x[i, j] == 1)
route[route.IndexOf(i)].Add(j);
}
I want to make if x[1,3] == 1 then I can add 3 to sublist which contains 1.
this code route.IndexOf(i) is still get red underline, please help how to correct it. Thanks
You can use LINQ's Single method to retrieve the specific list you want given a predicate Contains(i). Here I am looking for the list that contains 6, and adds 7 to it.
var routes = new List<List<int>>()
{
new List<int>() {1, 2, 3},
new List<int>() {4, 5, 6},
};
List<int> targetList = routes.Single(i => i.Contains(6));
targetList.Add(7);
To get specifically the index of that list, then you can use IndexOf method like:
int targetListIndex = routes.IndexOf(targetList); // 1 in this example
Let's start from an example (which we turn into a test later):
List<List<int>> route = new List<List<int>>() {
new List<int>() {1, 2, 3, 4, 5}, // sublist #0: contains value == 4
new List<int>() {7, 8, 2, 9}, // sublist #1: doesn't contain value == 4
new List<int>() {9, 10, 4}, // sublist #2: contains value == 4
};
and we are looking for value within each of sublists
int value = 4;
finally, we want to have sublist indexes as the outcome: 0 and 2.
If it's your case, I suggest using Linq
List<List<int>> route = new List<List<int>>() {
new List<int>() {1, 2, 3, 4, 5},
new List<int>() {7, 8, 2, 9},
new List<int>() {9, 10, 4},
};
int value = 4;
var indexesFound = route
.Select((sublist, index) => new { // eh, new class, but anonymous one
sublist = sublist,
index = index, })
.Where(chunk => chunk.sublist.Contains(value))
.Select(chunk => chunk.index)
.ToArray(); // if you want, say, array materialization
Test
Console.Wrire(string.Join(", ", indexesFound));
Outcome:
0, 2
Edit: if you want to have just one index, you have to specify which one, e.g. for the very first index put .First() instead of .ToArray():
int firstIndexFound = route
.Select((sublist, index) => new { // eh, new class, but anonymous one
sublist = sublist,
index = index, })
.Where(chunk => chunk.sublist.Contains(value))
.Select(chunk => chunk.index)
.First(); // or .Last()
Suppose I have a sorted array such as
int[] sarr = new int[] { 0, 1, 3, 5 };
and I want to iterate through all combinations of size K in ascending order of their sum. For example, if K=2 then the combinations in order are
{0, 1} (sum = 1)
{1, 0} (sum = 1)
{0, 3} (sum = 3)
{3, 0} (sum = 3)
{3, 1} (sum = 4)
{1, 3} (sum = 4)
{5, 0} (sum = 5)
.
.
.
I want to do this without first getting all combinations because I want to stop as soon as I've found one that satisfies a condition Func<int[],bool> cond.
Is there a known way of doing this?
I would use yield return to describe all combinations, arrangements or whatever subcollections you want to generate and then use FirstOrDefault on the result.
That way you will only generate all subcollections if there isn't one you are looking for or the one you are looking for is the very last.
On about getting them ascending by the sum of the elements, sort the initial collection and then pick k elements from start to end. You can even generate combinations and from these to generate all possible permutations and thus you will get all your arrangements.
A quick way to get all combinations:
class Program
{
static void Main(string[] args)
{
var initialArray = new[] { 0, 1, 3, 5 };
var subArrayLength = 2;
foreach (var subArray in GetSubArrays(initialArray, subArrayLength))
Console.WriteLine($"[{string.Join(", ", subArray)}]");
Console.WriteLine("Searching for array that contains both 1 and 5.");
var arrayFulfillingCriteria = GetSubArrays(initialArray, subArrayLength).FirstOrDefault(array => array.Contains(1) && array.Contains(5));
if (arrayFulfillingCriteria != null)
Console.WriteLine($"[{string.Join(", ", arrayFulfillingCriteria)}]");
else
Console.WriteLine("No array found.");
}
static IEnumerable<int[]> GetSubArrays(int[] initialArray, int subArrayLength)
{
var indexStack = new Stack<int>(Enumerable.Range(0, subArrayLength));
do
{
var subArray = indexStack.Select(i => initialArray[i]).Reverse().ToArray();
yield return subArray;
var index = indexStack.Pop();
while (indexStack.Count != 0 && indexStack.Count < subArrayLength && index == initialArray.Length - (subArrayLength - indexStack.Count))
index = indexStack.Pop();
while (indexStack.Count < subArrayLength && index < initialArray.Length - (subArrayLength - indexStack.Count))
{
index++;
indexStack.Push(index);
}
}
while (indexStack.Count != 0);
}
}
The only reason I can think of where you would need arrangements (seeing as you order by sum) is that the items within the sub array need to be in a particular order.
Does this work for you?
Func<IEnumerable<int>, IEnumerable<IEnumerable<int>>> getAllSubsets = null;
getAllSubsets = xs =>
(xs == null || !xs.Any())
? Enumerable.Empty<IEnumerable<int>>()
: xs.Skip(1).Any()
? getAllSubsets(xs.Skip(1))
.SelectMany(ys => new [] { ys, xs.Take(1).Concat(ys) })
: new [] { Enumerable.Empty<int>(), xs.Take(1) };
Now, given this:
Func<int[],bool> cond = xs => true;
int[] sarr = new int[] { 0, 1, 3, 5, };
var result =
getAllSubsets(sarr)
.Where(xs => xs.Count() == 2)
.Where(xs => cond(xs.ToArray()));
I get this as the result:
{0, 1}
{0, 3}
{1, 3}
{0, 5}
{1, 5}
{3, 5}
How can I find the set of items that occur in 2 or more sequences in a sequence of sequences?
In other words, I want the distinct values that occur in at least 2 of the passed in sequences.
Note:
This is not the intersect of all sequences but rather, the union of the intersect of all pairs of sequences.
Note 2:
The does not include the pair, or 2 combination, of a sequence with itself. That would be silly.
I have made an attempt myself,
public static IEnumerable<T> UnionOfIntersects<T>(
this IEnumerable<IEnumerable<T>> source)
{
var pairs =
from s1 in source
from s2 in source
select new { s1 , s2 };
var intersects = pairs
.Where(p => p.s1 != p.s2)
.Select(p => p.s1.Intersect(p.s2));
return intersects.SelectMany(i => i).Distinct();
}
but I'm concerned that this might be sub-optimal, I think it includes intersects of pair A, B and pair B, A which seems inefficient. I also think there might be a more efficient way to compound the sets as they are iterated.
I include some example input and output below:
{ { 1, 1, 2, 3, 4, 5, 7 }, { 5, 6, 7 }, { 2, 6, 7, 9 } , { 4 } }
returns
{ 2, 4, 5, 6, 7 }
and
{ { 1, 2, 3} } or { {} } or { }
returns
{ }
I'm looking for the best combination of readability and potential performance.
EDIT
I've performed some initial testing of the current answers, my code is here. Output below.
Original valid:True
DoomerOneLine valid:True
DoomerSqlLike valid:True
Svinja valid:True
Adricadar valid:True
Schmelter valid:True
Original 100000 iterations in 82ms
DoomerOneLine 100000 iterations in 58ms
DoomerSqlLike 100000 iterations in 82ms
Svinja 100000 iterations in 1039ms
Adricadar 100000 iterations in 879ms
Schmelter 100000 iterations in 9ms
At the moment, it looks as if Tim Schmelter's answer performs better by at least an order of magnitude.
// init sequences
var sequences = new int[][]
{
new int[] { 1, 2, 3, 4, 5, 7 },
new int[] { 5, 6, 7 },
new int[] { 2, 6, 7, 9 },
new int[] { 4 }
};
One-line way:
var result = sequences
.SelectMany(e => e.Distinct())
.GroupBy(e => e)
.Where(e => e.Count() > 1)
.Select(e => e.Key);
// result is { 2 4 5 7 6 }
Sql-like way (with ordering):
var result = (
from e in sequences.SelectMany(e => e.Distinct())
group e by e into g
where g.Count() > 1
orderby g.Key
select g.Key);
// result is { 2 4 5 6 7 }
May be fastest code (but not readable), complexity O(N):
var dic = new Dictionary<int, int>();
var subHash = new HashSet<int>();
int length = array.Length;
for (int i = 0; i < length; i++)
{
subHash.Clear();
int subLength = array[i].Length;
for (int j = 0; j < subLength; j++)
{
int n = array[i][j];
if (!subHash.Contains(n))
{
int counter;
if (dic.TryGetValue(n, out counter))
{
// duplicate
dic[n] = counter + 1;
}
else
{
// first occurance
dic[n] = 1;
}
}
else
{
// exclude duplucate in sub array
subHash.Add(n);
}
}
}
This should be very close to optimal - how "readable" it is depends on your taste. In my opinion it is also the most readable solution.
var seenElements = new HashSet<T>();
var repeatedElements = new HashSet<T>();
foreach (var list in source)
{
foreach (var element in list.Distinct())
{
if (seenElements.Contains(element))
{
repeatedElements.Add(element);
}
else
{
seenElements.Add(element);
}
}
}
return repeatedElements;
You can skip already Intesected sequences, this way will be a little faster.
public static IEnumerable<T> UnionOfIntersects<T>(this IEnumerable<IEnumerable<T>> source)
{
var result = new List<T>();
var sequences = source.ToList();
for (int sequenceIdx = 0; sequenceIdx < sequences.Count(); sequenceIdx++)
{
var sequence = sequences[sequenceIdx];
for (int targetSequenceIdx = sequenceIdx + 1; targetSequenceIdx < sequences.Count; targetSequenceIdx++)
{
var targetSequence = sequences[targetSequenceIdx];
var intersections = sequence.Intersect(targetSequence);
result.AddRange(intersections);
}
}
return result.Distinct();
}
How it works?
Input: {/*0*/ { 1, 2, 3, 4, 5, 7 } ,/*1*/ { 5, 6, 7 },/*2*/ { 2, 6, 7, 9 } , /*3*/{ 4 } }
Step 0: Intersect 0 with 1..3
Step 1: Intersect 1 with 2..3 (0 with 1 already has been intersected)
Step 2: Intersect 2 with 3 (0 with 2 and 1 with 2 already has been intersected)
Return: Distinct elements.
Result: { 2, 4, 5, 6, 7 }
You can test it with the below code
var lists = new List<List<int>>
{
new List<int> {1, 2, 3, 4, 5, 7},
new List<int> {5, 6, 7},
new List<int> {2, 6, 7, 9},
new List<int> {4 }
};
var result = lists.UnionOfIntersects();
You can try this approach, it might be more efficient and also allows to specify the minimum intersection-count and the comparer used:
public static IEnumerable<T> UnionOfIntersects<T>(this IEnumerable<IEnumerable<T>> source
, int minIntersectionCount
, IEqualityComparer<T> comparer = null)
{
if (comparer == null) comparer = EqualityComparer<T>.Default;
foreach (T item in source.SelectMany(s => s).Distinct(comparer))
{
int containedInHowManySequences = 0;
foreach (IEnumerable<T> seq in source)
{
bool contained = seq.Contains(item, comparer);
if (contained) containedInHowManySequences++;
if (containedInHowManySequences == minIntersectionCount)
{
yield return item;
break;
}
}
}
}
Some explaining words:
It enumerates all unique items in all sequences. Since Distinct is using a set this should be pretty efficient. That can help to speed up in case of many duplicates in all sequences.
The inner loop just looks into every sequence if the unique item is contained. Thefore it uses Enumerable.Contains which stops execution as soon as one item was found(so duplicates are no issue).
If the intersection-count reaches the minum intersection count this item is yielded and the next (unique) item is checked.
That should nail it:
int[][] test = { new int[] { 1, 2, 3, 4, 5, 7 }, new int[] { 5, 6, 7 }, new int[] { 2, 6, 7, 9 }, new int[] { 4 } };
var result = test.SelectMany(a => a.Distinct()).GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList();
First you make sure, there are no duplicates in each sequence. Then you join all sequences to a single sequence and look for duplicates as e.g. here.
Okay, so this seems simple, but I can't think of a straightforward solution;
Basically I have an object array in C# that contains, say, 102 elements. I then also have 4 other empty arrays. I want to iterate through the original array and distribute the 100 elements evenly, then distribute 101 and 102 to the 1st and 2nd new arrays respectively.
int i = 1,a=0, b=0, c=0, d = 0;
foreach (ReviewStatus data in routingData)
{
if (i == 1)
{
threadOneWork[a] = data;
a++;
}
if (i == 2)
{
threadTwoWork[b] = data;
b++;
}
if (i == 3)
{
threadThreeWork[c] = data;
c++;
}
if (i == 4)
{
threadFourWork[d] = data;
d++;
i = 0;
}
i++;
}
Now the above code definitely works, but I was curious, does anybody know of a better way to do this??
var workArrays = new[] {
threadOneWork,
threadTwoWork,
threadThreeWork,
threadFourWork,
};
for(int i=0; i<routingData.Length; i++) {
workArrays[i%4][i/4] = routingData[i];
}
Put the four arrays into an array of arrays, and use i%4 as an index. Assuming that thread###Work arrays have enough space to store the data, you can do this:
var tw = new[] {threadOneWork, threadTwoWork, threadThreeWork, threadFourWork};
var i = 0;
foreach (ReviewStatus data in routingData) {
tw[i%4][i/tw.Length] = data;
i++;
}
Linq is your friend! Use modulo to group the items via the total number of arrays in your case 4.
For example the code splits them up into four different lists:
var Items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Items.Select( ( i, index ) => new {
category = index % 4,
value = i
} )
.GroupBy( itm => itm.category, itm => itm.value )
.ToList()
.ForEach( gr => Console.WriteLine("Group {0} : {1}", gr.Key, string.Join(",", gr)));
/* output
Group 0 : 1,5,9
Group 1 : 2,6,10
Group 2 : 3,7
Group 3 : 4,8
*/