Check for duplicates

My console application will cycle through each user to get their websites so that he can take new screenshots from them. However, in order not to repeat the screenshot of the same website twice, I have to check if the screenshot of the website has already been taken and then scroll through other users websites.

My current solution:

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 Application:

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 if a specific URL has been added ... any suggestions for an alternative way?

+3
source share
1 answer

you can use

 var websitesUpdated = new HashSet<string>();

The cost of the operation is O (1) instead of O (n) in the list.

EDIT: , URL- HashSet, , HashSet, .

.

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

foreach (var website in websites)
{
Console.WriteLine(new string('-', 50)); 
Console.WriteLine("{0} has successfully been renewed",website.URL);
}
+2

Source: https://habr.com/ru/post/1794928/


All Articles