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<string, int> duplicateNames = files.GroupBy(x => x)        .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 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.
Correcting duplicate names in array
c#;beginner;algorithm;file
A quick and easy way would be to use a HashSet<T> 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<T>. 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<string>(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();}