Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
¿Cómo usar HashSet como un conjunto matemático?

Intento crear un conjunto matemático usando HashSet . Tengo el siguiente código:

 using System; using System.Collections.Generic; class Program { static void Main() { HashSet<int> A = new HashSet<int>() { 1, 2 }; HashSet<int> B = new HashSet<int>() { 1, 2 }; HashSet<HashSet<int>> SET = new HashSet<HashSet<int>>() { A, B }; // Desired: 1 (A and B are expected being equal) // Actual: 2 Console.WriteLine(SET.Count); Console.ReadKey(); } }

Parece que la igualdad HashSet no es adecuada porque A y B deben considerarse iguales, pero para HashSet son objetos diferentes. ¿Cómo puedo redefinir la igualdad para HashSet?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debe explicar a .Net cómo comparar sus datos personalizados ( HashSet<T> en su caso) con la ayuda de IEqualityComparer<T> :

 public sealed class HashSetComparer<T> : IEqualityComparer<HashSet<T>> { public bool Equals(HashSet<T>? left, HashSet<T>? right) { if (ReferenceEquals(left, right)) return true; if (left == null || right == null) return false; return left.SetEquals(right); } public int GetHashCode(HashSet<T> item) { //TODO: improve; simplest, but not that good implementation return item == null ? -1 : item.Count; } }

luego mencione las reglas de comparación al crear SET:

 ... HashSet<HashSet<int>> SET = new HashSet<HashSet<int>>(new HashSetComparer<int>()) { A, B }; ...
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!