Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check duplicate string array in list?

How to check duplicate string array in list?
I declare string array list like this:

List<string[]> list = new List<string[]>();

and I add a few items in the list.

list.Add(new string[3] {"1","2","3"});
list.Add(new string[3] {"2","3","4"});
list.Add(new string[1] {"3"});
list.Add(new string[1] {"3"});
list.Add(new string[3] {"1","2","3"});

now I want to get to know which items are duplicated. I tried like below to add the duplicated items to new list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (int j = 0; j < list.Count - 1; j++)
{
    for (int k = list.Count - 1; k > j; k--)
    {
        if (j != k)
        {
            if (Enumerable.SequenceEqual(list[j], list[k]))
            {
                savedDistinctList.Add(list[j]);
            }
        }
    }
}

and finally I want to remove the duplicated item in the first list. so I want to see 3 items in the list.([1,2,3],[2,3,4],[3])

Perhaps any idea using LINQ or something else?

>Solution :

First we have to teach .Net how to compare arrays:

private sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]> {
  public bool Equals(T[] left, T[] right) {
    if (ReferenceEquals(left, right))
      return true;
    if (left is null || right is null)
      return false;

    return left.SequenceEqual(right);
  }

  public int GetHashCode(T[] array) => array is null
    ? -1
    : array.Length;
} 

Then you can use Linq Distinct with this class implemented:

using System.Linq;

...

savedDistinctList = list
  .Distinct(new ArrayEqualityComparer<string>())
  .ToList();

If you want to modify the existing list, you can use HashSet<T>:

var unique = new HashSet<string[]>(new ArrayEqualityComparer<string>());

for (int i = list.Count - 1; i >= 0; --i)
  if (!unique.Add(list[i]))
    list.RemoveAt(i);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading