Add distinct items from a list to another list
Solution 1
OP's comments are vague.
Option 1: Unique numbers from across all multidimensional arrays
List<int> UniqueList = new List<int>();
UniqueList = LongList.Select(i => Flatten(i))
.SelectMany(i => i)
.Distinct()
.ToList();
This would turn { [[0, 1], [2, 3]], [[2, 2], [4, 5]] } to { 0, 1, 2, 3, 4, 5 }
See below for Flatten
Option 2: Unique multidimensional arrays by values
NB: Assumes size and number of dimensions of each multidimensional array match.
List<int[,]> UniqueList = new List<int[,]>();
foreach (var e in LongList)
{
IEnumerable<int> flat = Flatten(e);
if (!UniqueList.Any(i => Flatten(i).SequenceEqual(flat)))
{
UniqueList.Add(e);
}
}
This would turn { [[0, 1], [2, 3]], [[0, 1], [2, 3]], [[2, 2], [4, 5]] } to { [[0, 1], [2, 3]], [[2, 2], [4, 5]] }
See below for Flatten
Option 3: Unique references only
UniqueList = aList.Distinct().ToList();
NB: This was the original answer, for context on the comments.
Flatten Method
In all cases Flatten
is taken from Guffa's SO Answer
public static IEnumerable<T> Flatten<T>(T[,] items) {
for (int i = 0; i < items.GetLength(0); i++)
for (int j = 0; j < items.GetLength(1); j++)
yield return items[i, j];
}
Other options
If OP would like something else (e.g. flattenting List<int[,]>
to List<int[]>
or support for different sized multidimensional arrays) they will have to comment back.
Solution 2
Based on OP's update, we just need to remove duplicate references. So we do not need to compare on a per-value basis. Distinct
should do:
UniqueList = LongList.Distinct().ToList();
Related videos on Youtube
Glitchezz
Software Engineer undergraduate. Just trying to make my way into the World of coding!
Updated on June 04, 2022Comments
-
Glitchezz almost 2 years
I would like to accomplish what the title states but I don't know how to go about doing so.
I have 2 lists:
public List<int[,]> LongList = new List<int[,]>(); public List<int[,]> UniqueList = new List<int[,]>();
To further explain, here's a scenario:
Puzzles:
public int[,] puzzle1 = new int [3,3] { {1,2,3}, {8,4,0}, {7,6,5} }; //[1,2,3;8,4,0;7,6,5] public int[,] puzzle2 = new int [3,3] { {8,7,6}, {1,0,5}, {2,3,4} }; //[8,7,6;1,0,5;2,3,4] public int[,] puzzle3 = new int [3,3] { {7,6,3}, {1,0,2}, {8,4,5} }; //[7,6,3;1,0,2;8,4,5]
LongList contains:
LongList.Add(puzzle1); LongList.Add(puzzle1); LongList.Add(puzzle1); LongList.Add(puzzle1); LongList.Add(puzzle2); LongList.Add(puzzle2); LongList.Add(puzzle3); LongList.Add(puzzle3); LongList.Add(puzzle3);
I would like Unique list to hold the UNIQUE values from LongList. AS IF this happened:
UniqueList.Add(puzzle1); UniqueList.Add(puzzle2); UniqueList.Add(puzzle3);
As an equation: UniqueList = Distinct values from LongList
List is full of multiple reoccurring values & I would like to take only the unique ones and put them into
UniqueList
.I'm trying to complete a puzzle and the
LongList
will contain multiple references of the same same puzzle and more. To make it simple for case of discussion:LongList
values: 1,1,1,1,2,2,3,4,4,4,4,5,5I would like
UniqueList
to contain the puzzles: 1,2,3,4,5-
İsmet Alkan about 11 yearspossible duplicate: stackoverflow.com/questions/4639036/…
-
İsmet Alkan about 11 yearsyou can check out this question's answers, too: stackoverflow.com/questions/1388361/…
-
Julián Urbano about 11 years@IsmetAlkan it's not nearly duplicate
-
Julián Urbano about 11 yearsWill you have the same objects several times, or different objects with the same content?
-
Matt Mitchell about 11 yearsThe issue is that there's no inbuilt comparator that is appropriate, or even a good equality operation for multidimensional arrays. Leveraging some of the examples at stackoverflow.com/questions/4423318/how-to-compare-arrays-in-c perhaps might help.
-
İsmet Alkan about 11 years@caerolus I don't think it's exact duplicate too, I said possible as you see, but it's absolutely "nearly" duplicate, I think.
-
İsmet Alkan about 11 yearsI don't think he needs array comparison here, according to list values he gave now.
-
Matt Mitchell about 11 yearsAre you actually using multidimensional arrays? This doesn't make as much sense if you are
-
Julián Urbano about 11 yearsShouldn't the uniqueList be
List<int>
then? -
Glitchezz about 11 yearsYeah I using multidimensional arrays (I'm 100% sure), they hold the states for the puzzle I'm trying to solve. What do you suggest doesn't make sense?
-
Matt Mitchell about 11 yearsOr, if you wanted to flatten each
int[,]
individually, perhaps aList<int[]>
? -
Matt Mitchell about 11 yearsLet's say you have a two element list with: { [[0, 1], [2, 3]], [[2, 2], [4, 5]] }. Do you turn that into { 0, 1, 2, 3, 4, 5 } or { [ 0, 1, 2, 3 ], [ 2, 4, 5] } ?
-
Glitchezz about 11 yearsNeither I believe (forgive me if I'm wrong). Please check edited question.
-
Matt Mitchell about 11 yearsWhat happens if there's another puzzle (puzzle4) with exactly the same values as puzzle3? Should it be included?
-
Glitchezz about 11 yearsHi Matt, thank you for your patience (also caerolus). That scenario shouldn't arise so I don't believe it will be a problem.
-
-
Matt Mitchell about 11 yearsDepends on your definition of 'unique'. I ran a test in LINQPad and it matched my intuition. I'll grab the code and post back.
-
Julián Urbano about 11 yearsI just checked and it doesn't.
-
Glitchezz about 11 yearsBy unique I mean: List would contain values: 1,1,1,2,2,2,2,3,3,4,4,4,4,5 (made simple for explanation as they're actually multi dimensional results). And I only want UniqueList to hold: 1,2,3,4,5
-
Matt Mitchell about 11 yearsYeah it performs reference equality, not same value equality. Okay deleting my answer as it's incorrect for what the OP wants.
-
Matt Mitchell about 11 yearsI don't think your explanation is simplifying. It actually complicates it. Can you give a multi-D example of what you're after? It sounds like only the unique numbers across all multi-dimensional arrays? Or a set of multi-dimensional arrays that only contain the unique numbers in each corresponding source array?
-
Glitchezz about 11 yearsIm sorry, please have a look at my edited question. If you need more info please let me know!
-
Matt Mitchell about 11 yearshaha my first answer... I'm upvoting you anyway for being as patient with this as I was :-)
-
Julián Urbano about 11 yearsI was just thinking about it...update yours and let that be the answer :-) btw...I'm not sure if it's really just references or he just made that example to keep it simple...
-
Matt Mitchell about 11 yearsBased on his comment that it "contains multiple references of the same puzzle" you sound like you've interpreted it right to me.
-
Julián Urbano about 11 yearsnot sure, really. +1 to you too and let him decide what he means :-)
-
Glitchezz about 11 yearsThanks a lot guys! If I were to call upon the unique list like so: UniqueList[0] UniqueList[1] UniqueList[2] I would need that to grab puzzle1, puzzle2 & puzzle 3 (with NO duplicates that the LongList holds).. Does this code provide that?