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

Combine two arrays without duplicate values

Learn how to combine two arrays without duplicate values in C# using the Union() method.

Example: Combine String Arrays
string[] animals = { "Cat", "Alligator", "Fox", "Donkey", "Cat" };
string[] birds = { "Sparrow", "Peacock", "Dove", "Crow" };
 
var all = animals.Union(birds).ToArray();
Try it

In the same way, use the Union() method with the number array.

Example:
int[] num1 = { 1, 2, 3, 4, 3, 55, 23, 2 };           
int[] num2 = { 55, 23, 45, 50, 80 };
 
var all = num1.Union(num2).ToArray();
Try it

If an array contains objects of a custom class, then you need to implement IEquatable<T> or IEqualityComparer<T>, as shown below.

Example: Implement IEquatable
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, you can use the Union() method, as shown below.

Example: Combine Object Arrays
Person[] people1 = {
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Bill", LastName="Gates"},
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Lary", LastName="Page"}
    };
 
    Person[] people2 = {
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Lary", LastName="Page"},
        new Person(){ FirstName="Bill", LastName="Gates"}
    };
    
var allp = people1.Union(people2).ToArray();
Array.ForEach(allp, p => Console.WriteLine(p.FirstName));
Try it
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.