CSharp · Console
using System;
using System.Collections.Generic;
string [] mots = new []
{
"bonjour", "coucou", "allo", "coucou", "yo", "bonjour", "coucou"
};
foreach(var (Mot, Nb) in CompterOccurrences(mots))
Console.WriteLine($"Le mot {Mot} apparaît {Nb} fois");
static int TrouverIndice(List<(string Mot, int Nb)> mots, string mot)
{
for(int i = 0; i != mots.Count; ++i)
if(mots[i].Mot == mot)
return i;
return -1;
}
static (string Nom, int Nb)[] CompterOccurrences(string [] mots)
{
List <(string Mot, int Nb)> lst = new ();
foreach(string mot in mots)
{
int ndx = TrouverIndice(lst, mot);
if(ndx == -1)
lst.Add((mot, 1));
else
lst[ndx] = (mot, lst[ndx].Nb + 1);
}
return lst.ToArray();
}