Help with code implementation - HashSet | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with code implementation - HashSet

This exercise I don't quite understand how I should do it. I can insert a foreach that goes through the names with a Key and later implement an If Else statement, it can be with a ternary operator and finally implement an IsSubsetOf to return true if the hash set is a superset of the specified collection. The exercise mentions the following: We are hiring programmers on our team. There are 10 candidates, and we need to choose 3 of them. In the program you are given, you have 10 candidates in a hash set. You need to take 3 names as input, add them to a new hash set and check if they are present in our candidate set. If they are, the program should generate "Starting hiring process", otherwise "Something is wrong". Input example John Susan Daniel Output example Starting hiring process I need help with this exercise. This is the code: https://code.sololearn.com/cfYUaGlwlfGm/?ref=app

9th Jul 2021, 7:15 PM
Alex Narváez
Alex Narváez - avatar
14 Answers
+ 3
https://code.sololearn.com/cJSOlwkCnjB8/?ref=app
9th Jul 2021, 8:47 PM
visph
visph - avatar
+ 7
Alex Narváez , ok - i got it thanks. what you need to do is: ▪︎ inside the existing while loop you have to add each of the 3 inputs to the HashSet < hiring > ▪︎after this you have to check if HashSet < hiring > is a subset of HashSet < candidates > or not happy coding and good success!
9th Jul 2021, 7:40 PM
Lothar
Lothar - avatar
+ 5
Alex Narváez , please give us a hint about the tutorial name and the number of lesson. thanks!
9th Jul 2021, 7:30 PM
Lothar
Lothar - avatar
+ 3
If it requires all 3 to be present in candidates set , then maybe use IntersectWith on Input set and check if count is still 3 for it , otherwise use IsSubsetOf !
9th Jul 2021, 7:41 PM
Abhay
Abhay - avatar
+ 3
Alex Narváez You already have a HashSet hiring so don't make another HashSet. Just add input in hiring. Btw if you make new HashSet inside loop then on every iteration new HashSet will create. So you have wrongly created new HashSet inside loop so remove that and just add items in HashSet hiring.
9th Jul 2021, 8:47 PM
A͢J
A͢J - avatar
+ 2
visph Thank you very much for the help, that was what I was missing. Now I understand how to do it.
9th Jul 2021, 8:53 PM
Alex Narváez
Alex Narváez - avatar
+ 2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { HashSet<string> candidates = new HashSet<string>(); candidates.Add("John"); candidates.Add("Amelie"); candidates.Add("Tom"); candidates.Add("Richard"); candidates.Add("Barbara"); candidates.Add("Susan"); candidates.Add("Charles"); candidates.Add("Daniel"); candidates.Add("Tamara"); candidates.Add("Donald"); HashSet<string> hiring = new HashSet<string>(); while (hiring.Count<3) { //add the recruiting names to the hash set hiring.Add(Console.ReadLine()); } //your code goes here if(hiring.IsSubsetOf(candidates)) { Console.WriteLine("Starting hiring process"); } else { Console.WriteLine("Something is wrong"); } } } }
11th Nov 2022, 3:26 PM
Pooja Patel
Pooja Patel - avatar
+ 1
Lothar Module: Generics. Lesson: Dictionary and HashSet. Exercise: Hiring Engineers
9th Jul 2021, 7:32 PM
Alex Narváez
Alex Narváez - avatar
+ 1
Thanks for your answers, they have told me in theory how to do it, which is something I have in mind, however I am looking for a way to implement it in code, which is what they have not told me
9th Jul 2021, 7:43 PM
Alex Narváez
Alex Narváez - avatar
+ 1
Alex Narváez You have a HashSet of candidates. Now make another HashSet with name hiring and add all inputs in the new HashSet. And then check new HashSet data in old HashSet data like this: if(hiring.IsSubsetOf(candidates)) which will return true. If it is true then print "Starting hiring process"
9th Jul 2021, 7:45 PM
A͢J
A͢J - avatar
+ 1
Lothar give you all you have to do and I ᴀᴍ "Tɪᴍᴇ" give you the conditional to use... don't forgot to add an else statement to print "Something is wrong"
9th Jul 2021, 7:57 PM
visph
visph - avatar
+ 1
I updated the code and so far I have what was recommended to me, however I need to correct some things. Can you help me, please
9th Jul 2021, 8:41 PM
Alex Narváez
Alex Narváez - avatar
0
What do you think? visph
9th Jul 2021, 7:48 PM
Alex Narváez
Alex Narváez - avatar
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { HashSet<string> candidates = new HashSet<string>(); candidates.Add("John"); candidates.Add("Amelie"); candidates.Add("Tom"); candidates.Add("Richard"); candidates.Add("Barbara"); candidates.Add("Susan"); candidates.Add("Charles"); candidates.Add("Daniel"); candidates.Add("Tamara"); candidates.Add("Donald"); HashSet<string> hiring = new HashSet<string>(); while (hiring.Count<3) { //add the recruiting names to the hash set hiring.Add(Console.ReadLine()); } //your code goes here if(hiring.IsSubsetOf(candidates)) { Console.WriteLine("Starting hiring process"); } else { Console.WriteLine("Something is wrong"); } } } }
28th Jul 2023, 8:59 PM
Melta