5 Replies - 5736 Views - Last Post: 17 January 2013 - 01:52 PM Rate Topic: -----

#1 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • 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.


TOP most asked:
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



Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,473
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • 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";
		}
		outputList.Add(s);
	}
	return outputList;
}


Was This Post Helpful? 2
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3251
  • View blog
  • Posts: 10,902
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1