Home > Software engineering >  Unique collection set [duplicate]
Unique collection set [duplicate]

Time:09-23

Is there a collection type that is like a table, but each row/set in the table is unique kind of like a database unique constraint?

| "txt" | "data"  |
| "txt" | "txt"   |
| "txt" | "data"  | // Duplicate row
| "data"| "txt"   | // Duplicate row
| "exe" | "path"  |
| "exe" | "path2" |
| "exe" | "path"  | // Duplicate row

CodePudding user response:

Expanding on the comments, if you want the row to function as an aggregate hash instead of just as a key, you would want to create a custom IEqualityComparer<(string, string)> and pass it to a HashSet.

public class ValueTupleEqualityComparer : IEqualityComparer<(string s1, string s2)>
{
    public bool Equals((string s1, string s2) other)
    {
        return base.Equals(other);
    }

    public bool Equals((string s1, string s2) x, (string s1, string s2) y)
    {
        if (object.ReferenceEquals(x, y))
            return true;

        if (x.s1 == y.s1 && x.s2 == y.s2)
            return true;

        if (x.s1 == y.s2 && x.s2 == y.s1)
            return true;

        return false;
    }

    public int GetHashCode((string s1, string s2) obj)
    {
        return base.GetHashCode();
    }
}

When testing this against your data set:

var hashSet = new HashSet<(string s1, string s2)>(new ValueTupleEqualityComparer());

hashSet.Add(("txt", "data"));
hashSet.Add(("txt", "txt"));
hashSet.Add(("txt", "data"));
hashSet.Add(("data", "txt"));
hashSet.Add(("exe", "path"));
hashSet.Add(("exe", "path2"));
hashSet.Add(("exe", "path"));

the following results are outputted:

txt data 
txt txt 
exe path 
exe path2 

This gives you unique results where both the "column1" and "column2" are used, regardless of order.

CodePudding user response:

If you only care about one column, you can use a Dictionary where the key has to be unique; otherwise, you can use a HashSet.

Note that

A HashSet collection is not sorted and cannot contain duplicate elements. If order or element duplication is more important than performance for your application, consider using the List class together with the Sort method.

Personally, I found Dictionary to be very useful as it is very fast to read. Also, there is a thread-safe ConcurrentDictionary that saves a lot of time and debugging when working with threads.

  •  Tags:  
  • c#
  • Related