diff --git a/DEV.3.2/TP/TP1-Generecite/Association.java b/DEV.3.2/TP/TP1-Generecite/Association.java new file mode 100644 index 0000000..c195ba1 --- /dev/null +++ b/DEV.3.2/TP/TP1-Generecite/Association.java @@ -0,0 +1,31 @@ +public class Association { + private E element; + private int frequence; + + public Association(E element, int frequence) { + this.element = element; + this.frequence = frequence; + } + + public E getElement() { + return element; + } + + public void setElement(E elt) { + this.element = elt; + } + + public int getFrequence() { + return frequence; + } + + public void setFrequence(int f) { + this.frequence = f; + } + + + @Override + public String toString() { + return "Association [element=" + element + ", frequence=" + frequence + "]"; + } +} \ No newline at end of file diff --git a/DEV.3.2/TP/TP1-Generecite/Main.java b/DEV.3.2/TP/TP1-Generecite/Main.java new file mode 100644 index 0000000..cfe8ef4 --- /dev/null +++ b/DEV.3.2/TP/TP1-Generecite/Main.java @@ -0,0 +1,34 @@ +import java.util.Map; +import java.util.HashMap; + + +public class Main { + + public static Association elementPlusFrequent(T[] tab) { + java.util.Map compteur = new java.util.HashMap<>(); + + for (T elem : tab) { + compteur.put(elem, compteur.getOrDefault(elem, 0) + 1); + } + + T resultat = null; + int maxFreq = 0; + for (T elem : tab) { + int freq = compteur.get(elem); + if (freq > maxFreq) { + maxFreq = freq; + resultat = elem; + } + } + + return new Association<>(resultat, maxFreq); + } + + public static void main(String[] args) { + String[] mots = {"chat", "chien", "chat", "oiseau", "chien", "chat"}; + System.out.println(elementPlusFrequent(mots)); + + Integer[] nombres = {1, 2, 2, 3, 1, 2}; + System.out.println(elementPlusFrequent(nombres)); + } +}