Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

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();
Try it

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();
Try it

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();
Try it

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();
Try it

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

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.