Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
3.60/5 (3 votes)
See more:
Hello all,

I need to write a code which is compares given the strings and numbers each string depending on if they are same or not. The given strings count can differ so it has to be generic. Here is an example:

HTML
Example 1:
Given strings: 
-XXX
-YYY
-ZZZ
-XXX

So the output should be
-1
-2
-3
-1
Example 2:
Given strings: 

-YYY
-XXX
-YYY
-ZZZ
-XXX
-ZZZ

So the output should be
-1
-2
-1
-3
-2
-3


So what I do so far I can find the how many different strings there are but I fail to do the versioning.
C#
var count = new Dictionary<string, int>();
foreach (string s in arrStrings)
{
    if (count.ContainsKey(s.ToString()))
    {
        count[s.ToString()]++;
    }
    else
    {
        count.Add(s.ToString(), 1);
    }
}
foreach (var item in count)
{
    Console.WriteLine("{0} : {1}", item.Key, item.Value);
}


Can someone advise me? Thanks in advance.
Posted
Comments
phil.o 18-Sep-13 4:01am    
Remark: why s.ToString() since s is already of String type?

Keep a dictionary with the string as key, and it's index as value, but only set the value if the key is not already in the dictionary.

Something like this might work for you;

C#
using System;
using System.Collections.Generic;
using System.Linq;

namespace Test {
  public class Program {
    public static void Main() {
      var strings = new[] {"YYY", "XXX", "YYY", "ZZZ", "XXX", "ZZZ"};
      var versionMap = new Dictionary<string, int>();

      foreach (var s in strings) {
        if (!versionMap.ContainsKey(s))
          versionMap[s] = versionMap.Any() ? versionMap.Values.Max() + 1 : 1;
      }

      foreach (var s in strings)
        Console.WriteLine("{0} : {1}", s, versionMap[s]);
    }
  }
}


Hope this helps,
Fredrik
 
Share this answer
 
Hi, I would do something like this:

C#
var count = new Dictionary<string, int>();
var version = new Dictionary<string, int>();
foreach (string s in arrStrings)
{
    if (count.ContainsKey(s.ToString()))
    {
        count[s.ToString()]++;
    }
    else
    {
        count.Add(s.ToString(), 1);
        version.Add(s, version.Count + 1);
    }
}
foreach (var item in count)
{
    Console.WriteLine("{0} : {1}", item.Key, item.Value);
}
Console.WriteLine("############");
foreach (var item in version)
{
    Console.WriteLine("{0} : {1}", item.Key, item.Value);
}
 
Share this answer
 
Using a dictionary is a good idea. Try the following code
C#
var di = new Dictionary<string, int>();

string[] sa = { "YYY", "XXX", "YYY", "ZZZ", "XXX", "ZZZ" };
int[] id = new int[sa.Length];
int cur_id = 1;

for (int n = 0; n<sa.Length ;++n)
{
  if (di.ContainsKey(sa[n]))
  {
    id[n] = di[sa[n]];
  }
  else
  {
    di.Add(sa[n], cur_id);
    id[n] = cur_id;
    ++cur_id;
  }
}
 
Share this answer
 
Comments
wonder-FOOL 18-Sep-13 4:50am    
Thank you for your valuable help.
CPallini 18-Sep-13 4:59am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900