Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How To Sequentially Number Duplicate Words In List

$
0
0
My objective is to sequentially number all of the duplicates in the list starting with the number 1 (while keeping the original order of the list the same).

For example the following method will take the list of names beneath-
DupeName
DupeName
SingleName
DupeName

- and renamed them to-
DupeName1
DupeName2
SingleName
DupeName3

It works with small lists fine, but when I run a longer list it sometimes appends "11" on the end of some the names.
I realize it's adding the text string of "1" + "1" but I don't understand why and how to fix it.

I don't know if has anything to do with it being a longer list versus the shorter lists that work fine,
Or if it's the data (although the data is not formatted any differently than the example above).

I was hoping someone could help me investigate what's wrong with my method,
Or show me a more efficient way to achieve my goal.

Any help is vastly appreciated as always.

private static List<string> NumberDuplicates(List<string> inputList)
{
	List<string> outputList = new List<string>();
	for (int aLoop = 0; aLoop < inputList.Count; aLoop++)
	{
		int duplicateCount = 0;
		string duplicateName = null;
		// I concatenate the list as a string to count dupes without using a loop.
		string concatenatedList = string.Join(Space, inputList.ToArray());
		for (int bLoop = 0; bLoop < inputList.Count; bLoop++)
		{
			duplicateCount = Regex.Matches(concatenatedList, inputList[bLoop] + Space).Count;
			if (duplicateCount > 1)
			{
				duplicateName = inputList[bLoop];
				break;
			}
		}
		int numberSuffix = 1;
		for (int cLoop = 0; cLoop < inputList.Count; cLoop++)
		{
			if (inputList[cLoop] == duplicateName)
			{
				inputList[cLoop] += numberSuffix.ToString();
				numberSuffix++;
			}
		}
		outputList = inputList;
	}
	return outputList;
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>