Using Dictionary Data type count the number of books written by each author
Book 1 - Author A
Book 2 - Author B
Book 3 - Author C
Book 4 - Author A
Book 5 - Author C
@Abhilash_S
Welcome to the forum
we cannot derive the input details from the description (Text, File, String, DataTable…)
But in general we would group the data and count the members
Assign Activity:
dictAuthorReport | Dictionary(Of String, int32)
(From x in .....(your Input)
Group x by k=...(YourAuthorInfo) into grp=Group
Select g = grp).ToDictionary(Function (g) g.Key, Function (g) g.Count)
Hi @Abhilash_S
Assign:
Dictionary = New Dictionary(Of String, String) From {
{"Book 1", "Author A"},
{"Book 2", "Author B"},
{"Book 3", "Author C"},
{"Book 4", "Author A"},
{"Book 5", "Author C"}
}
Note: Dictionary is of DataType System.Collections.Generic.Dictionary(System.String,System.String)
Use the below code in Invoke Code:
Dim authorBookCount As New Dictionary(Of String, Integer)
For Each pair In booksAndAuthors
Dim author As String = pair.Value
If authorBookCount.ContainsKey(author) Then
authorBookCount(author) += 1
Else
authorBookCount(author) = 1
End If
Next
Display the results
For Each authorCountPair In authorBookCount
Console.WriteLine($"{authorCountPair.Key}: {authorCountPair.Value} books")
Next
Regards
2 Likes
Hi @Abhilash_S
You’re Welcome!!
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.


