I searched, but I found only answers which related to two lists. But what about when they are more than two?
List 1 = 1,2,3,4,5
List 2 = 6,7,8,9,1
List 3 = 3,6,9,2,0,1
List 4 = 1,2,9,0,5
List 5 = 1,7,8,6,5,4
List 6 = 1
List 7 =
How to get the common items? as you can see one of them is empty, so the common will be empty, but I need to skip empty lists.
var data = new List<List<int>> {
new List<int> {1, 2, 3, 4, 5},
new List<int> {6, 7, 2, 8, 9, 1},
new List<int> {3, 6, 9, 2, 0, 1},
new List<int> {1, 2, 9, 0, 5},
new List<int> {1, 7, 8, 6, 2, 5, 4},
new List<int> {1, 7, 2}
};
List<int> res = data
.Aggregate<IEnumerable<int>>((a, b) => a.Intersect(b))
.ToList();
The type of Aggregate is explicitly given, otherwise aggregation of two Lists would have to be List too. It can be easily adapted to run in parallel:
List<int> res = data
.AsParallel<IEnumerable<int>>()
.Aggregate((a, b) => a.Intersect(b))
.ToList();
EDIT
Except... it does not run in parallel. The problem is operations on IEnumerable are deferred, so even if they are logically merged in parallel context, the actual merging occurs in the ToList(), which is single threaded. For parallel execution it would be better to leave IEnumerable and return to the Lists:
List<int> res = data
.AsParallel()
.Aggregate((a, b) => a.Intersect(b).ToList());
You can chain Intersect:
List<int> List1 = new List<int> {1, 2, 3, 4, 5};
List<int> List2 = new List<int> { 6, 7, 8, 9, 1 };
List<int> List3 = new List<int> { 3, 6, 9, 2, 0, 1 };
List<int> List4 = new List<int> { 1, 2, 9, 0, 5 };
List<int> List5 = new List<int> { 1, 7, 8, 6, 5, 4 };
List<int> List6 = new List<int> { 1 };
List<int> common = List1
.Intersect(List2)
.Intersect(List3)
.Intersect(List4)
.Intersect(List5)
.Intersect(List6)
.ToList();
var data = new [] {
new List<int> {1, 2, 3, 4, 5},
new List<int> {6, 7, 8, 9, 1},
new List<int> {3, 6, 9, 2, 0, 1},
new List<int> {1, 2, 9, 0, 5},
new List<int> {1, 7, 8, 6, 5, 4},
new List<int> {1},
new List<int> {},
null
};
IEnumerable<int> temp = null;
foreach (var arr in data)
if (arr != null && arr.Count != 0)
temp = temp == null ? arr : arr.Intersect(temp);
One way is to use a HashSet. You can put the items of the first collection in the hash, then iterate each collection after the first and create an new hash that you add items from the current collection to if it's in the hash. At the end you assign that common hash set to the overall one and break if it's every empty. At the end you just return the overall hash set.
public IEnumerable<T> CommonItems<T>(IEnumerable<IEnumerable<T>> collections)
{
if(collections == null)
throw new ArgumentNullException(nameof(collections));
using(var enumerator = collections.GetEnumerator())
{
if(!enumerator.MoveNext())
return Enumerable<T>.Empty();
var overall = new HashSet<T>(enumerator.Current);
while(enumerator.MoveNext())
{
var common = new HashSet<T>();
foreach(var item in enumerator.Current)
{
if(hash.Contains(item))
common.Add(item);
}
overall = common;
if(overall.Count == 0)
break;
}
return overall;
}
}
Related
How can I take n elements from a m elements collection so that if I run out of elements it starts from the beginning?
List<int> list = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<int> newList = list.Skip(9).Take(2).ToList();
List<int> expected = new List(){10,1};
CollectionAssert.AreEqual(expected, newList);
How can I get the expected list?
I'm looking for a CircularTake() function or something in that direction.
use an extension method to circular repeat the enumerable
public static IEnumerable<T> Circular<T>( this IEnumerable<T> source )
{
while (true)
foreach (var item in source)
yield return item;
}
and you can use your code
List<int> list = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<int> newList = list.Circular().Skip(9).Take(2).ToList();
.net fiddle example
You don't need to track the overflow because we can use the % modulus operator (which returns the remainder from an integer division) to continually loop through a range of indexes, and it will always return a valid index in the collection, wrapping back to 0 when it gets to the end (and this will work for multiple wraps around the end of the list):
List<int> list = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<int> newList = new List<int>();
for (int skip = 9, take = 2; take > 0; skip++, take--)
{
newList.Add(list[skip % list.Count]);
}
Result:
// newList == { 10, 1 }
This could be extracted into an extension method:
public static List<T> SkipTakeWrap<T>(this List<T> source, int skip, int take)
{
var newList = new List<T>();
while (take > 0)
{
newList.Add(source[skip % source.Count]);
skip++;
take--;
}
return newList;
}
And then it could be called like:
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> newList = list.SkipTakeWrap(9, 2);
you may need to do something like this
var start = 9;
var amount = 2;
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> listOverflow = list.ToList();
var overflow = (start + amount) - list.Count;
if (overflow > 0)
for (var i = 0; i < overflow; i++)
listOverflow.AddRange(list.ToList());
var newList = listOverflow.Skip(start).Take(amount).ToList();
My take on a CircularTake extension.
public static IEnumerable<T> CircularTake<T>(this IReadOnlyList<T> source, int count)
{
return Enumerable.Range(0, count).Select(i => source[i % source.Count]);
}
int overflow = take - (elements.Count - skip);
if(overflow > 0)
{
results.AddRange(elements.Skip(skip).Take(take - overflow));
results.AddRange(elements.Take(overflow));
}
If there is a possiblity that there are more than one circular iterations, e.g. from 3 elements, take 10 or more, then you can apply this logic in a recursive function.
In python there is a functionality (numpy.take) to sort arrays within an array, for example if I have an array (3x3):
a = [[1, 2, 3],[7,9,10],[3, 5,6]]
and I have an array of set indices
indices = [2, 0, 1]
the result shall be
array([[ 3, 5, 6], [ 1, 2, 3], [ 7, 9, 10]]).
Are there any direct approach methods/ functions as these in C# where I can pass in a jagged array and produce the same output?
Not directly, but you can achieve the same thing with Linq
var a = new[] { new[] { 1, 2, 3 }, new[] { 7, 9, 10 }, new[] { 3, 5, 6 } };
var indices = new [] { 2, 0, 1 };
var sorted = indices.Select(i => a[i]).ToArray();
foreach(var s in sorted) Console.WriteLine(string.Join(", ", s));
Note this does not check that your indices are all in range.
You can do it easily with LINQ:
var a = new[] { new[] { 1, 2, 3 }, new[] { 7, 9, 10 }, new[] { 3, 5, 6 } };
var indices = new[] { 2, 0, 1};
var result = indices
.Select(i => a[i])
.ToArray();
Or .ToList() if you prefer lists.
There is also the Array.Sort(keys, values) - MSDN
var a = new[]
{
new[] {1, 2, 3},
new[] {7, 9, 10},
new[] {3, 5, 6}
};
var indices = new[] {2, 0, 1};
var sortedArray = a.SortEx(indices);
Where SortEx is
public static class Extensions
{
public static T[][] SortEx<T>(this T[][] source, int[] indices)
{
return indices.Select(index => source[index]).ToArray();
}
}
This assumes that the all the indices in the indices array are not out of bound in a.
I have seen other people having a similar problem, but no solution has been able help me with mine.
I was looking for a way to make:
List<int> Factors = new List<int> {2, 5, 5, 7, 2, 3};
return:
{7, 3};
Is there any way that this can be done?
Using GroupBy you can group the numbers and get those groups that has only one number:
Factors = Factors.GroupBy(x => x)
.Where(g => g.Count() == 1)
.Select(g => g.Key)
.ToList();
Probably a Non LINQ solution. Add the numbers in list as key to the hashtable. in next iteration if the same key found in hashtable then increase it's corresponding value. so, at end you will remain with only those numbers which have only one appearance.
static void Main(string[] args)
{
List<int> Factors = new List<int> { 2, 5, 5, 7, 2, 3, 2, 9, 8, 9, 11, 9, 12, 9, 13, 9 };
Hashtable ht = new Hashtable();
foreach (var item in Factors)
{
if (ht.ContainsKey(item))
{
ht[item] = Convert.ToInt32(ht[item]) + 1;
}
else
ht.Add(item, 1);
}
foreach (var item in ht.Keys)
{
if (Convert.ToInt32(ht[item]) == 1)
Console.WriteLine(item.ToString());
}
}
I have already looked at some array topics but I am still stumped.
I wish to add a new line to my jagged array - and am strugigng to get the syntax right..
int[][] intJaggedArray = new int[7][];
intJaggedArray[0] = new int[3] { 1, 1, 1 };
intJaggedArray[1] = new int[3] { 2, 2, 2 };
intJaggedArray[2] = new int[3] { 3, 3, 3 };
intJaggedArray[3] = new int[3] { 4, 4, 4 };
intJaggedArray[4] = new int[3] { 5, 5, 5 };
intJaggedArray[5] = new int[3] { 6, 6, 6 };
intJaggedArray[6] = new int[3] { 7, 7, 7 };
So now if i want to add
intJaggedArray[0] = new int[3] { 1, 1, 2 };
so the array ends up being as shown below how do I acheive it - thanks in advance - A noob from England. (And a big thanks in advance)
intJaggedArray[0] = new int[3] { 1, 1, 1 };
intJaggedArray[0] = new int[3] { 1, 1, 2 };
intJaggedArray[1] = new int[3] { 2, 2, 2 };
intJaggedArray[2] = new int[3] { 3, 3, 3 };
intJaggedArray[3] = new int[3] { 4, 4, 4 };
intJaggedArray[4] = new int[3] { 5, 5, 5 };
intJaggedArray[5] = new int[3] { 6, 6, 6 };
intJaggedArray[6] = new int[3] { 7, 7, 7 };
What do you want to do? Insert a line between 0 and 1? Or replace the existing line 0?
Your line :
intJaggedArray[0] = new int[3] { 1, 1, 2 };
simply replaces the existing line 0.
You can't insert a line in an array. To do so, use a list instead:
List<int[]> myList = new List<int[]>();
myList.Add(new int[] {...});
myList.Add(new int[] {...});
myList.Add(new int[] {...});
...
myList.Insert(1, new int[] {...});
Or if you want to replace the existing line, then simply:
You might want to create a collection or a List<int[]>
Then you could insert an item into it at a certain index.
List<int[]> x = new List<int[]>();
x.Insert(3, new int[3] { 1, 2, 3 });
If you want the initial list to be of a variable length, you cannot use an array. Use a List instead.
This should work:
List<int[]> intJaggedList = new List<int[]>();
intJaggedList.Add( new int[3] { 1, 1, 1 } );
intJAggedList.Add( new int[3] { 2, 2, 2 } );
...
Then to insert your new array:
intJaggedList.Insert( 1, new int[3] { 1, 1, 2 } );
I've got a list of lists which I want to intersect:
List<List<int>> input = new List<List<int>>();
input.Add(new List<int>() { 1, 2, 4, 5, 8 });
input.Add(new List<int>() { 3, 4, 5 });
input.Add(new List<int>() { 1, 4, 5, 6 });
Output should be:
{ 4, 5 }
How can this be accomplished in a terse fashion?
var result = input.Cast<IEnumerable<int>>().Aggregate((x, y) => x.Intersect(y))