# How To Sequentially Number Duplicate Words In List

Page 1 of 1

## 5 Replies - 11549 Views - Last Post: 17 January 2013 - 01:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307737&amp;s=2b14119460e8a96d2605cc45bcb1fc52&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Alyssa Saila

Reputation: 0
• Posts: 72
• Joined: 07-January 12

# How To Sequentially Number Duplicate Words In List

Posted 17 January 2013 - 01:13 PM

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;
}

```

Is This A Good Question/Topic? 0

## Replies To: How To Sequentially Number Duplicate Words In List

### #2 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6528
• Posts: 14,423
• Joined: 02-June 10

## Re: How To Sequentially Number Duplicate Words In List

Posted 17 January 2013 - 01:22 PM

What this shows us is that you aren't familiar with breakpoints and how to debug your own code.

tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: How To Sequentially Number Duplicate Words In List

Posted 17 January 2013 - 01:24 PM

You would really be better off using a Dictionary<string, int> to relate the Strings with the number of times they occurred.

### #4 baavgai

• Dreaming Coder

Reputation: 7119
• Posts: 14,839
• Joined: 16-October 07

## Re: How To Sequentially Number Duplicate Words In List

Posted 17 January 2013 - 01:38 PM

That join to string thing is messy. I don't see anything immediately wrong, but that kind of smells.

The putting 1 of the first one messes with more elegant solutions, since you have to go back and tick it off.

I'd count once and use a dictionary. e.g.
```private static List<string> NumberDuplicates(List<string> inputList) {
List<string> outputList = new List<string>();
Dictionary<string, int> counts = new Dictionary<string,int>();
for(int i=0; i<inputList.Count; i++) {
var s = inputList[i];
if (counts.ContainsKey(s)) {
s += ++counts[s];
} else if (inputList.FindIndex(i+1, (sm)=> { return s.Equals(sm); })!=-1) {
counts[s] = 1;
s += "1";
}
}
return outputList;
}

```

### #5 andrewsw

• blow up my boots

Reputation: 6524
• Posts: 26,403
• Joined: 12-December 12

## Re: How To Sequentially Number Duplicate Words In List

Posted 17 January 2013 - 01:49 PM

OOPs IGNORE ME! You're numbering them.. not counting; my bad!!

This post has been edited by andrewsw: 17 January 2013 - 02:07 PM

### #6 Alyssa Saila

Reputation: 0
• Posts: 72
• Joined: 07-January 12

## Re: How To Sequentially Number Duplicate Words In List

Posted 17 January 2013 - 01:52 PM

baavgai Thank You!

I'm not exactly sure how it works at this moment but I'm studying it now.
I don't have that much experience with Dictionaries but I'm going get this down and start working with them more often. Thanks for the reply and the provided revisions. You've helped me before when I really needed it in the past. If I can't walk I know you're not gonna push me out the chair and tell me I can't walk because I don't know how. Lol Thanks again.