Tutorialsteacher

Follow Us

How to remove duplicate values from an array

Removing duplicate values from an array in C# is essentially getting distinct values. In C#, we cannot remove values in the array. Instead, we will have to create a new array with the values we want. So, we have to get the distinct values from the specified array and create a new array of distinct values instead of removing duplicate values.

The following example gets distinct values from an array using the Distinct() method and creates a new array.

Example: Remove duplicate from integer array
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2 };
int[] dist = nums.Distinct().ToArray();

The same can be used with the string array.

Example: Remove duplicate from string array
string[] animals = { "Cat", "Alligator", "Fox", "Donkey", "Cat" };
 
string[]  dist = animals.Distinct().ToArray();

To remove duplicate values and get distinct values from an object array, we need to implement either IEquatable<T> or IEqualityComparer<T>.

The following example gets a distinct array of the Person array.

Example: Remove duplicate from object array
Person[] people = {
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Bill", LastName="Gates"},
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Lary", LastName="Page"}
    };
           
var dist = people.Distinct(new PersonNameComparer()).ToArray();

The following class implements IEqualityComparer<T> to be used with the Distinct() method.

Example: IEqualityComparer<T>
class PersonNameComparer : IEqualityComparer<Person>
{
       
    public bool Equals(Person x, Person y)
    {
        return x.FirstName == y.FirstName && x.LastName == y.LastName;
    }
 
    public int GetHashCode(Person obj)
    {
        return obj.Id.GetHashCode() ^ (obj.FirstName == null ? 0 : obj.FirstName.GetHashCode()) ^ (obj.LastName == null ? 0 :obj.LastName.GetHashCode());
    }
}

In the above Equals() method, we compare FirstName and LastName. You may compare IDs also. It's upto you how you want to consider it equal.

You may also implement IEquatable<T> in the Person class itself to get the same result.

Example: IEquatable<T>
class Person : IEquatable<Person>
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
 
    public bool Equals(Person other)
    {
        return FirstName.Equals(other.FirstName) && LastName.Equals(other.LastName);
    }
 
    public override int GetHashCode()
    {
        return Id.GetHashCode() ^ (FirstName == null ? 0 : FirstName.GetHashCode()) ^ (LastName == null ? 0 : LastName.GetHashCode());
    }
}

Now, we can get distinct persons without passing a parameter in the Distinct() method, as shown below.

Example: Get distinct values from object array
Person[] people = {
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Bill", LastName="Gates"},
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Lary", LastName="Page"}
    };
 
var dist = people.Distinct().ToArray();

Thus, we can get the distinct values and remove duplicate values from the array in C#.