patterncsharpMinor
Correcting duplicate names in generic array - follow up
Viewed 0 times
genericcorrectingduplicatearraynamesfollow
Problem
Follow Up question of : Correcting duplicate names in array
I have an array of file names. For example:
Model:
And I've developed the following method to correct duplicate names. What the method does is just change duplicate names. I am just adding incremented value for the next duplicated value. For example, my developed method
And implementation of
```
private FileContent[] ChangingDuplicateNames(FileContent[] files)
{
//Creating a dicitonary to store duplicated values. "Key" of dictionary
//is duplicated name, "Value" of dictionary is number to add for name
Dictionary duplicateNames = files.GroupBy(x => x.Name)
.Where(group => group.Count() > 1)
.ToDictionary(grouped => grouped.Key, grouped => 0);
if (duplicateNames.Count == 0)
return files;
int namesLength = files.Length;
string actualName = string.Empty;
f
I have an array of file names. For example:
FileContent[] files =
{
new FileContent() {Content = threeItems, Name = "one.zip" },
new FileContent() {Content = fiveItems, Name = "one.zip" },
new FileContent() {Content = sevenItems, Name = "one.zip" },
new FileContent() {Content = threeItems, Name = "two.zip" },
new FileContent() {Content = fiveItems, Name = "two.zip" },
new FileContent() {Content = sevenItems, Name = "two.zip" },
};Model:
public sealed class FileContent
{
public byte[] Content { get; set; }
public string Name { get; set; }
}And I've developed the following method to correct duplicate names. What the method does is just change duplicate names. I am just adding incremented value for the next duplicated value. For example, my developed method
ChangingDuplicateNames(string[] files)correct the previous array to:FileContent[] files =
{
new FileContent() {Content = threeItems, Name = "one.zip" },
new FileContent() {Content = fiveItems, Name = "one(1).zip" },
new FileContent() {Content = sevenItems, Name = "one(2).zip" },
new FileContent() {Content = threeItems, Name = "two.zip" },
new FileContent() {Content = fiveItems, Name = "two(1).zip" },
new FileContent() {Content = sevenItems, Name = "two(2).zip" },
};And implementation of
ChangingDuplicateNames(FileContent[] files) is:```
private FileContent[] ChangingDuplicateNames(FileContent[] files)
{
//Creating a dicitonary to store duplicated values. "Key" of dictionary
//is duplicated name, "Value" of dictionary is number to add for name
Dictionary duplicateNames = files.GroupBy(x => x.Name)
.Where(group => group.Count() > 1)
.ToDictionary(grouped => grouped.Key, grouped => 0);
if (duplicateNames.Count == 0)
return files;
int namesLength = files.Length;
string actualName = string.Empty;
f
Solution
Because we are dealing now with objects, we need to create a copy of each
Instead of having
In addition, we are using the
and call it like so
FileContent to achieve the same result like in your previous question. Instead of having
var currentFile = file where file had been a string, we now use var currentFile = new FileContent() { Content = file.Content, Name = file.Name };. In addition, we are using the
HashSet only for look up and return an IEnumerable instead of an array. private IEnumerable ChangingDuplicateNames(FileContent[] files)
{
var hashSet = new HashSet(StringComparer.OrdinalIgnoreCase);
foreach (var file in files)
{
var currentFile = new FileContent() { Content = file.Content, Name = file.Name };
int counter = 0;
while (!hashSet.Add(currentFile.Name))
{
currentFile.Name = CreateFileName(file.Name, ref counter);
}
yield return currentFile;
}
}and call it like so
FileContent[] result = ChangingDuplicateNames(files).ToArray();Code Snippets
private IEnumerable<FileContent> ChangingDuplicateNames(FileContent[] files)
{
var hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var file in files)
{
var currentFile = new FileContent() { Content = file.Content, Name = file.Name };
int counter = 0;
while (!hashSet.Add(currentFile.Name))
{
currentFile.Name = CreateFileName(file.Name, ref counter);
}
yield return currentFile;
}
}FileContent[] result = ChangingDuplicateNames(files).ToArray();Context
StackExchange Code Review Q#162593, answer score: 3
Revisions (0)
No revisions yet.