1

I tried this code for if i have duplicate string in multiple arraylist it update 1_Maths,2_Maths

e.g. in multiplearraylist i have

maths

english

maths

hindi

english

so i want o/p:

1_Maths

1_english

2_Maths hindi

2_english

My code is this for generate array list

Dim col As Integer = 0
Dim dt As New DataTable

Dim ArraySubject(0, Grd.Columns.Count - 1) As String
For i As Integer = 0 To Grd.Columns.Count - 1
   If Grd.Columns(i).Visible = True Then
      ArraySubject(0, col) = GvSearch.HeaderRow.Cells(i).Text 
      col += 1
    End If
Next

Answer in c# would also help me.

3
  • How do you get "2_Maths hindi"??? This is a terrible, terrible question. Extremely unclear. Commented Aug 3, 2013 at 6:35
  • @ThomasW It seems formatting problem I just corrected Commented Aug 3, 2013 at 6:39
  • What you tried for achieving this post that too Commented Aug 3, 2013 at 6:40

2 Answers 2

2

I don't know much VB.NET, so here's what I think you want in C#:

string[] subjects = new string[] { "maths", "english", "maths", "hindi", "english" };

Dictionary<string, int> subjectCounts = new Dictionary<string, int> ();
foreach (string subject in subjects) {
    if (subjectCounts.ContainsKey(subject))
        subjectCounts[subject]++;
    else
        subjectCounts.Add(subject, 1);
}

List<string> output = new List<string>();
foreach (KeyValuePair<string, int> pair in subjectCounts) {
    if (pair.Value > 1) {
        for (int i = 1; i <= pair.Value; i++)
            output.Add(i + "_" + pair.Key);
    } else {
        output.Add(pair.Key);
    }
}

foreach (string subject in output)
    Console.WriteLine(subject);

This outputs:

1_maths

2_maths

1_english

2_english

hindi

If you want it sorted by number, just sort the array before outputting it.

output.Sort();

Output:

1_english

1_maths

2_english

2_maths

hindi

Sign up to request clarification or add additional context in comments.

1 Comment

foreach (KeyValuePair<string, int> pair in subjectCounts) { if (pair.Value > 1) { for (int i = 1; i <= pair.Value; i++) output.Add(i + "_" + pair.Key); } else { output.Add(pair.Key); } }This loop giving error for subjectCounts is not convert type string to double
2
private List<string> list = new List<string>();
private List<string> rslt = new List<string>();            
list.Add("math");
list.Add("science");
list.Add("math");
list.Add("science");

foreach (string i in list)
{
     rslt.Add(i);
}
foreach (string i in list)
{
     if (list.Count<string>(f => f == i) > 1)
     {
          int cnt = 1;
          int idx = 0;
          foreach (string j in list)
          {
               if (j == i)
               {
                    rslt[idx] = cnt.ToString() + "_" + j;
                    cnt++;
               }
               idx++;
           }
      }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.