Follow Up question of : Correcting duplicate names in array
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<string, int> 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;
for (int indexArray = 0; indexArray < namesLength; indexArray++)
{
int value;
bool isDuplicate = duplicateNames
.TryGetValue(files[indexArray].Name, out value);
if (isDuplicate)
{
actualName = files[indexArray].Name;
if (value == 0)
files[indexArray].Name = files[indexArray].Name;
else
{
//Adding increment to the mext duplicate name
string fileNameWithoutExtension = Path
.GetFileNameWithoutExtension(files[indexArray].Name);
string fileExtension = Path
.GetExtension(files[indexArray].Name);
files[indexArray].Name = fileNameWithoutExtension + "(" + value + ")"
+ fileExtension;
}
duplicateNames[actualName] = ++value;
}
}
return files;
}
And my question is: Is it possible to improve this algorithm? I mean could be this code smaller?
Maybe I should not iterate through the all array of names, but I cannot figure out how I can change names in files
without iterating through all array. Thanks in advance.
files[indexArray].Name = files[indexArray].Name;
- really? (Then, I'd prefercurrent
orname
overcurrentName
overactualName
. Rather than assigning a descriptively named (conceptual) const (isDuplicate
), I'd define a predicate…) Don't know aboutimprove … smaller
, but why iterate the names twice? \$\endgroup\$