How to remove duplicate data when calling AddTest
, remove duplicates (before adding to the list
) ??
Contains need to be added, but how do I do that with List ?
private static List<KeyValuePair<byte[], string>> list = new List<KeyValuePair<byte[], string>>();
public static void AddTest(byte[] myarray, string test)
{
list.Add(new KeyValuePair<byte[], string>(myarray, test));
}
CodePudding user response:
First of all, we should come to terms: what are duplicates of
KeyValuePair<byte[], string>
instances. Assuming that KeyValuePair<byte[], string>
instances x
and y
are duplicate if and only if both Key
s and Value
s are equal, i.e.
x.Key.SequenceEquals(y.Key) && x.Value == y.Value
we can implement a required comparer:
public sealed class MyEquComparer : IEqualityComparer<KeyValuePair<byte[], string>> {
public bool Equals(KeyValuePair<byte[], string> x, KeyValuePair<byte[], string> y) =>
Enumerable.SequenceEqual(x.Key, y.Key) && string.Equals(x.Value, y.Value);
public int GetHashCode([DisallowNull] KeyValuePair<byte[], string> obj) =>
obj.Key == null ? 0 : obj.Key.Length;
}
And then using the comparer get rid of duplicates:
private static List<KeyValuePair<byte[], string>> list =
new List<KeyValuePair<byte[], string>>();
private static HashSet<KeyValuePair<byte[], string>> unique = new
HashSet<KeyValuePair<byte[], string>>(new MyEquComparer());
public static void AddTest(byte[] myarray, string test)
{
// only if we're adding a unique item, we put it into the list
if (unique.Add(test))
list.Add(new KeyValuePair<byte[], string>(myarray, test));
}
CodePudding user response:
Consider using Dictionary. It allows use TryAdd that dont do anything if the dictionary already contains the Key(also it doesnt throw an Exception).
Further Informations: Here
CodePudding user response:
Why on earth are you using a 'List<KeyValuePair<byte[], string>>' when you could just use the Dictionary<T,T> where you determine T as your types?
Part of your problem is that you are not using the appropriate types. A List does not prevent duplicates from being added. But what you could do is check that list to see if the Key Value Pair you are adding already exists. Although this whole issue can be prevented by using TryAdd method from switching out your List<T,T> for a Dictionary.
TryAdd returns true if the key/value pair was added to the dictionary successfully; otherwise, false. Microsoft says :
Unlike the Add method, this method doesn't throw an exception if the element with the given key exists in the dictionary. Unlike the Dictionary indexer, TryAdd doesn't override the element if the element with the given key exists in the dictionary. If the key already exists, TryAdd does nothing and returns false.
And that will prevent your duplicate items from being added. More info can be read more on this here in the docs : https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-5.0