Ad Code

Responsive Advertisement

Console App Task 18: How to merge filesUpToYesterday and filesUpToYesterdayDyName into allFiles while avoiding duplicates in C#

Console App Task 18: How to merge filesUpToYesterday and filesUpToYesterdayDyName into allFiles while avoiding duplicates in C# ?

To avoid adding duplicate file paths to the `allFiles` list, you can use several approaches that ensure each file path is unique. Here are some methods:


Method 1: Using `HashSet`

A `HashSet` inherently prevents duplicate entries. You can add your file paths to a `HashSet` first and then convert it back to a list if necessary.

// Create a HashSet to automatically handle duplicates
HashSet<string> allFilesSet = new HashSet<string>();

// Add files to the HashSet
allFilesSet.UnionWith(filesUpToYesterday);
allFilesSet.UnionWith(filesUpToYesterdayDyName);

// Convert HashSet back to a List if needed
List<string> allFiles = allFilesSet.ToList();

if (allFiles.Count > 0)
{
    foreach (string filePath in allFiles)
    {
        // Your logic here
        Console.WriteLine(filePath);
    }
}

Method 2: Using LINQ `Distinct`

LINQ's `Distinct` method can be used to remove duplicates after combining the lists.

// Combine both lists and use Distinct to remove duplicates
var allFiles = filesUpToYesterday.Concat(filesUpToYesterdayDyName).Distinct().ToList();

if (allFiles.Count > 0)
{
    foreach (string filePath in allFiles)
    {
        // Your logic here
        Console.WriteLine(filePath);
    }
}

Method 3: Using a Custom Method

You can also write a custom method to add elements to a list only if they are not already present.

List<string> allFiles = new List<string>();

void AddUniqueFiles(List<string> targetList, List<string> sourceList)
{
    foreach (var file in sourceList)
    {
        if (!targetList.Contains(file))
        {
            targetList.Add(file);
        }
    }
}

// Add unique files from both lists
AddUniqueFiles(allFiles, filesUpToYesterday);
AddUniqueFiles(allFiles, filesUpToYesterdayDyName);

if (allFiles.Count > 0)
{
    foreach (string filePath in allFiles)
    {
        // Your logic here
        Console.WriteLine(filePath);
    }
}

Method 4: Using `AddRange` with a Check

Before adding each file, check if it already exists in the target list.

List<string> allFiles = new List<string>();

void AddFilesAvoidingDuplicates(List<string> targetList, List<string> sourceList)
{
    foreach (var file in sourceList)
    {
        if (!targetList.Contains(file))
        {
            targetList.Add(file);
        }
    }
}

// Add unique files from both lists
AddFilesAvoidingDuplicates(allFiles, filesUpToYesterday);
AddFilesAvoidingDuplicates(allFiles, filesUpToYesterdayDyName);

if (allFiles.Count > 0)
{
    foreach (string filePath in allFiles)
    {
        // Your logic here
        Console.WriteLine(filePath);
    }
}

Summary

- `HashSet`: Efficiently handles duplicates and is easy to use.
- LINQ `Distinct`: A clean, one-liner approach but might be less performant with very large lists.
- Custom Method: Provides flexibility and can be tailored for specific needs.
- `AddRange` with a Check: Simple and straightforward for small lists.

Choose the method that best suits your application's needs and performance requirements.

If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement