using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// List of words to group
var words = new List<string>
{
"Python", "JavaScript", "C", "Java", "C#", "PHP",
"C++", "Pascal", "SQL", "Rust"
};
var grouped = GroupByFirstLetter(words);
// Print each group
foreach (var entry in grouped) {
Console.WriteLine($"{entry.Key}: [{string.Join(", ", entry.Value)}]");
}
}
/// <summary>
/// Groups a list of words by their first letter.
/// </summary>
/// <param name="words">List of words to group</param>
/// <returns>
/// Dictionary where each key is a character and each value is a list of words.
/// </returns>
static Dictionary<char, List<string>> GroupByFirstLetter(List<string> words) {
// Dictionary that maps a character to a list of words
var groups = new Dictionary<char, List<string>>();
// Loop through each word
foreach (var word in words) {
char firstLetter = word[0]; // Extract the first letter
// If the key doesn't exist, create a new list
if (!groups.ContainsKey(firstLetter)) {
groups[firstLetter] = new List<string>();
}
// Add the word to the appropriate list
groups[firstLetter].Add(word);
}
return groups;
}
}
/*
run:
P: [Python, PHP, Pascal]
J: [JavaScript, Java]
C: [C, C#, C++]
S: [SQL]
R: [Rust]
*/