0

I have List<List<int>> object

var lists = new List<List<int>>
{
    new List<int> { 20 },
    new List<int> { 10, 10 },
    new List<int> { 5, 15 },
    //next two lists should be considered as equals and 
    //one of this should be removed from main list
    new List<int> { 2, 18 },
    new List<int> { 18, 2 },
};

Now I want to remove duplicate from the lists. For example, result list should remove one (4th or 5th list), and contain only four lists.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Ruslan_K
  • 423
  • 7
  • 23

3 Answers3

2

All the arithmetics like 20, 10 + 10, 5 + 15, 2 + 18 and 18 + 2 will be computed at the compile time, so at the run time you can't distinguish 20's from one another.

However, you may change design from sums (18 + 2) into just tems (18, 2):

// please, notice commas instead of +'s 
var lists = new List<List<int>>() {
  new List<int> { 20 },
  new List<int> { 10, 10 },
  new List<int> {  5, 15 },
  new List<int> {  2, 18 },
  new List<int> { 18,  2 },
}; 

In this case case you can implement duplicates eliminations

// simplest, providing that list doesn't contain null's
for (int i = 0; i < lists.Count; ++i) {
  // since we want to compare sequecnes, we shall ensure the same order of their items
  var item = lists[i].OrderBy(x => x).ToArray();

  for (int j = lists.Count - 1; j > i; --j)
    if (item.SequenceEqual(lists[j].OrderBy(x => x)))
      lists.RemoveAt(j);
}

Test

var result = lists.Select(line => string.Join(" + ", line));
Console.Write(string.Join(Environment.NewLine, result));

The output is

20
10 + 10
5 + 15
2 + 18
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

If possible, consider reviewing the problem that you are trying to solve. The complexity could potentially be reduced to the List at least.

One of the easiest way to ensure that the collection contains unique values is to use HashSet.

See: Collection that allows only unique items in .NET?

Community
  • 1
  • 1
0

Firstly, before you go on searching how to remove duplicates inside list of lists, I guess you should get a better understanding about what a list is , and how is it represented.

Consider the following statement:

var list = new List<int> { 10 + 10 };

What happens here is that the arithmetic operation (10 + 10) is executed before the list is constructed so you get an equivalent statement:

var list = new List<int> { 20 };

This is a list with a single element 20. So are all your other lists.


Now let me assume this is not what you wanted, and what you want is that you to instantiate lists with all the elements in the curly braces as being part of the list. In order to do this you have to separe them by comma, so the compiler will now they are separate elements, and not to use the sum operator, which actually summs them.

var list = new List<int> { 10, 10 };

This statement creates a list with two elements 10 and 10.

They are various ways to do this, but for now I guess you should get familiar with how the lists work and then you should move on and find that the answer you are looking for is here.

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76