3

Answer in c# also help me.

I tried this code for if i have duplicate string in multiple arraylist it update and display in sequence as before.

maths
english
maths
hindi
english
science
Economics
scince

i need output like this

maths_1
english_1
maths_2
hindi
science_1
Economics
scince_2

i tried this code but output is not in sequence**

Dim subjectCounts As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
            For Each subject As String In arraysub
                If subjectCounts.ContainsKey(subject) Then
                    subjectCounts(subject) = (subjectCounts(subject) + 1)
                Else
                    subjectCounts.Add(subject, 1)
                End If
            Next
            Dim output As List(Of String) = New List(Of String)

            For Each pair As KeyValuePair(Of String, Integer) In subjectCounts
                If (pair.Value > 1) Then
                    Dim i As Integer = 1
                    Do While (i <= pair.Value)
                        output.Add((i.ToString + ("_" + pair.Key)))

                        i = (i + 1)
                    Loop
                Else
                    output.Add(pair.Key)
                End If
            Next
4
  • what is your question? Commented Aug 5, 2013 at 6:15
  • Why did you flag this with C# and asp.net? And wat do you mean by not in sequence? Commented Aug 5, 2013 at 6:15
  • Answer in c# also help me. Commented Aug 5, 2013 at 6:17
  • my subjectname is in arraylist so if i have two subject with same name then i want to update that arraylist.but sequence of arraylist is not change Commented Aug 5, 2013 at 6:42

3 Answers 3

2

I think this generates the output you want

First let us check if the subject need to have the "_#" ending

now we run throught the subject, and add the _# ending for everyone that has more then one occurence. The order will be the same as the input, since we run through it. The counting will generated on the fly, so this will be correct.

    Dim hasMultiple As New Dictionary(Of String, Boolean)
    For Each subject As String In arraysub
        If hasMultiple.ContainsKey(subject) Then
            hasMultiple(subject) =  True
        Else
            hasMultiple.Add(subject, False)
        End If
    Next

    Dim output As New List(Of String)
    Dim subCount As New Dictionary(Of String, Integer) 
    For Each subject As String In arraysub
        If Not subCount.ContainsKey(subject) Then
            subCount.Add(subject, 0)
        End If
        subCount(subject) += 1
        If hasMultiple(subject) Then
            output.Add(subject & "_" & subCount(subject))
        Else
            output.Add(subject)
        End If
    Next
Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is in using a dictionary. dictionaries aren't ordered so there's no guarantee of the order whenever you iterate through it. However a List(Of KeyValuePair(Of String,Integer)) will do the job you want.

Additionally you can do it using the same list(Of String). I don't use arraylist's much, never found a need that a list can't do, but I imagine the syntax should be pretty much the same. Something like this should work

Dim arraysub As List(Of String) = New List(Of String)({
"maths",
"english",
"maths",
"hindi",
"english",
"science",
"Economics",
"science"
})
For i = 0 To arraysub.Count - 1
    If Not Char.IsDigit(arraysub(i).Last) Then
        Dim temp As String = arraysub(i)
        For j = 0 To arraysub.FindAll(Function(s) s = arraysub(i)).Count - 1
            arraysub(arraysub.IndexOf(temp)) += "_" + (j + 1).ToString
        Next
    End If
Next

The output is:

?arraysub
Count = 8
    (0): "maths_1"
    (1): "english_1"
    (2): "maths_2"
    (3): "hindi_1"
    (4): "english_2"
    (5): "science_1"
    (6): "Economics_1"
    (7): "science_2"

Here's the same code using an arraylist:

    Dim arraysub As ArrayList = New ArrayList(New String(7) {"maths", "english", "maths", "hindi", "english", "science", "Economics", "science"})
    For i = 0 To arraysub.Count - 1
        If Not Char.IsDigit(CStr(arraysub(i)).Last) Then
            Dim temp As String = CStr(arraysub(i))
            For j = 0 To Array.FindAll(arraysub.ToArray, Function(s) s Is CStr(arraysub(i))).Count - 1
                arraysub(arraysub.IndexOf(temp)) = CStr(CStr(arraysub(arraysub.IndexOf(temp))) & "_" + (j + 1).ToString)
            Next
        End If
    Next

and has this output:

?arraysub
Count = 8
    (0): "maths_1" {String}
    (1): "english_1" {String}
    (2): "maths_2" {String}
    (3): "hindi_1" {String}
    (4): "english_2" {String}
    (5): "science_1" {String}
    (6): "Economics_1" {String}
    (7): "science_2" {String}

you can see from this why many people prefer List over ArrayList.

26 Comments

i don't want two array list.I want to update the array in same arraylist .. I want full code.
Using this list replaces the dictionary and doesn't add an extra collection.
my subjectname is in arraylist so if i have two subject with same name then i want to update that arraylist.but sequence of arraylist is not change
sir i using ur code but it gives me error msg FindAll is not member of arraylist
Findall is method of arraylist or not?
|
1

Check this post by Charles Bretana

Create class like this,

     public class MultiDimDictList<K, T>: Dictionary<K, List<T>>  
       {
           public void Add(K key, T addObject)
           {
               if(!ContainsKey(key)) 
               {
               Add(key, new List<T>());
               base[key].Add(addObject);
               }else{
               for(int i=1; i<i+1;i++){
                if(!ContainsKey(key+"_"+i)){
                    Add(key+"_"+i+, new List<T>());
                    base[key+"_"+i].Add(addObject+"_"+i);       
                    break;
                  }
                }
              }  
           }           
       }

call it like below,

myDicList.Add("YourKEY", "YourSUBJECT");

I just modified as per your requirement, but i'm not sure about this.

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.