Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for child duplicates

My console app will loop through each User to get their Websites, so that it can take new screenshots of them. However, to prevent taking screenshot of the same website twice I have to check whether there already has been taken screenshot of the website, while looping through another users websites.

My current solution is:

Database:

User
|--> ID: 1
|--> FirstName: Joe

|--> ID: 2
|--> FirstName: Stranger

Websites
|--> ID: 1
|--> UserID: 1
|--> URL: http://site.com

|--> ID: 2
|--> UserID: 2
|--> URL: http://site.com

Console app:

static void RenewWebsiteThumbNails()
{
    Console.WriteLine("Starting renewal process...");

    using (_repository)
    {
        var websitesUpdated = new List<string>();

        foreach (var user in _repository.GetAll())
        {
            foreach (var website in user.Websites.Where(website => !websitesUpdated.Contains(website.URL)))
            {
                _repository.TakeScreenDumpAndSave(website.URL);
                websitesUpdated.Add(website.URL);

                Console.WriteLine(new string('-', 50));
                Console.WriteLine("{0} has successfully been renewed", website.URL);
            }
        }
    }
}

However, it seems wrong to declare a List for such a scenario, just to check whether a specific URL already has been added... any suggestions for an alternative way?

like image 501
ebb Avatar asked Mar 31 '26 22:03

ebb


1 Answers

You can use

 var websitesUpdated = new HashSet<string>();

Cost of the operation O(1) instead of O(n) in list case.

EDIT: By the way I would take all urls from each user and put them all in one HashSet so there won't be any duplicates and then just iterate on HashSet as it is a simple list.

Some think like this.

var websites = new HashSet<string>();
foreach (var url in   _repository.GetAll().SelectMany(user=>user.Websites))
  websites.Add(url);

After this,

foreach (var website in websites)
{
Console.WriteLine(new string('-', 50)); 
Console.WriteLine("{0} has successfully been renewed",website.URL);
}
like image 108
Eugeniu Torica Avatar answered Apr 03 '26 14:04

Eugeniu Torica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!