Add distinct items from a list to another list

11,765

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();
Share:
11,765

Related videos on Youtube

Glitchezz
Author by

Glitchezz

Software Engineer undergraduate. Just trying to make my way into the World of coding!

Updated on June 04, 2022

Comments

  • Glitchezz
    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,5

    I would like UniqueList to contain the puzzles: 1,2,3,4,5

    • İsmet Alkan
      İsmet Alkan about 11 years
    • İsmet Alkan
      İsmet Alkan about 11 years
      you can check out this question's answers, too: stackoverflow.com/questions/1388361/…
    • Julián Urbano
      Julián Urbano about 11 years
      @IsmetAlkan it's not nearly duplicate
    • Julián Urbano
      Julián Urbano about 11 years
      Will you have the same objects several times, or different objects with the same content?
    • Matt Mitchell
      Matt Mitchell about 11 years
      The 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
      İ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
      İsmet Alkan about 11 years
      I don't think he needs array comparison here, according to list values he gave now.
    • Matt Mitchell
      Matt Mitchell about 11 years
      Are you actually using multidimensional arrays? This doesn't make as much sense if you are
    • Julián Urbano
      Julián Urbano about 11 years
      Shouldn't the uniqueList be List<int> then?
    • Glitchezz
      Glitchezz about 11 years
      Yeah 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
      Matt Mitchell about 11 years
      Or, if you wanted to flatten each int[,] individually, perhaps a List<int[]>?
    • Matt Mitchell
      Matt Mitchell about 11 years
      Let'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
      Glitchezz about 11 years
      Neither I believe (forgive me if I'm wrong). Please check edited question.
    • Matt Mitchell
      Matt Mitchell about 11 years
      What happens if there's another puzzle (puzzle4) with exactly the same values as puzzle3? Should it be included?
    • Glitchezz
      Glitchezz about 11 years
      Hi 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
    Matt Mitchell about 11 years
    Depends 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
    Julián Urbano about 11 years
    I just checked and it doesn't.
  • Glitchezz
    Glitchezz about 11 years
    By 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
    Matt Mitchell about 11 years
    Yeah it performs reference equality, not same value equality. Okay deleting my answer as it's incorrect for what the OP wants.
  • Matt Mitchell
    Matt Mitchell about 11 years
    I 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
    Glitchezz about 11 years
    Im sorry, please have a look at my edited question. If you need more info please let me know!
  • Matt Mitchell
    Matt Mitchell about 11 years
    haha my first answer... I'm upvoting you anyway for being as patient with this as I was :-)
  • Julián Urbano
    Julián Urbano about 11 years
    I 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
    Matt Mitchell about 11 years
    Based 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
    Julián Urbano about 11 years
    not sure, really. +1 to you too and let him decide what he means :-)
  • Glitchezz
    Glitchezz about 11 years
    Thanks 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?