Correcting duplicate names in array I have an array of file names. For example:

string[] files= { "one.zip", "two.txt", "one.zip", "three.txt", "three.txt", "one.zip" };


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:

string[] files= { "one.zip", "two.txt", "one1.zip", "three.txt", "three1.txt", "one2.zip"};


And implementation of ChangingDuplicateNames(string[] files) is:

private string[] ChangingDuplicateNames(string[] files)
{
    //Creating a dicitonary to store duplicated values. "Key" of dictionary        
    //is duplicated name, "Value" of dictionary is number to add for name
    Dictionary&lt;string, int&gt; duplicateNames = files.GroupBy(x =&gt; x)
        .Where(group =&gt; group.Count() &gt; 1)
        .ToDictionary(grouped =&gt; grouped.Key, grouped =&gt; 0);

    if (duplicateNames.Count == 0)
        return files;

    int namesLength = files.Length;
    string actualName = string.Empty;
    for (int indexArray = 0; indexArray &lt; namesLength; indexArray++)
    {
        int incrementedValue;
        bool isDuplicate = duplicateNames.TryGetValue(files[indexArray], 
            out incrementedValue);
        if (isDuplicate)
        {
           actualName = files[indexArray];
           if (incrementedValue == 0)
              files[indexArray] = files[indexArray];
           else
           {
               //Adding increment to the mext duplicate name
               string fileNameWithoutExtension = Path
                    .GetFileNameWithoutExtension(files[indexArray]);
               string fileExtension = Path
                   .GetExtension(files[indexArray]);
               files[indexArray] = fileNameWithoutExtension + 
                   "(" + incrementedValue + ")" + fileExtension;
           }
           duplicateNames[actualName] = ++incrementedValue;
        }
    }
    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.
A quick and easy way would be to use a HashSet&lt;T&gt; and check the return of the Add() method. If it returns false you will know that you need to create a new filename. So let's start by extracting the creation  of the filename like so  

private string CreateFileName(string original, ref int counter)
{
    counter += 1;
    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(original) + "(" + counter + ")";
    string fileExtension = Path.GetExtension(original);
    return fileNameWithoutExtension + fileExtension;
}  


Next we iterate over all of the items of the array and try to add it to the HashSet&lt;T&gt;. If we can't add it, we create the new filename and try to add it again until we succeed.

private string[] ChangingDuplicateNames(string[] files)
{
    var hashSet = new HashSet&lt;string&gt;(StringComparer.OrdinalIgnoreCase);
    foreach (var file in files)
    {
        var currentFile = file;
        int counter = 0;
        while (!hashSet.Add(currentFile))
        {
            currentFile = CreateFileName(file, ref counter);
        }
    }

    return hashSet.ToArray();
}