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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With