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,5
I would like UniqueList
to contain the puzzles: 1,2,3,4,5
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 (eg flattenting List<int[,]>
to List<int[]>
or support for different sized multidimensional arrays) they will have to comment back.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.