class Frekvens> { class Node { Node neste = null; T data; int antall = 1; Node(T n) { data = n; } } Node forste = null, siste = null; T flest; int flestAntall; T hentFlest() { return flest; } int hentAntall() { return flestAntall; } void add(T x) { if (forste == null) { forste = siste = new Node(x); } else { siste.neste = siste = new Node(x); } } void komprimer() { Node p1 = forste, p2 = forste.neste; while (p2 != null) { if (p1.data.compareTo(p2.data) == 0) { p1.antall++; p1.neste = p2.neste; } else { p1 = p2; } p2 = p2.neste; } } void finnFlest() { flest = null; flestAntall = 0; Node p = forste; while (p != null) { if (p.antall > flestAntall) { flest = p.data; flestAntall = p.antall; } p = p.neste; } } void visData() { Node p = forste; while (p != null) { System.out.print(p.data + "(" + p.antall + ") "); p = p.neste; } System.out.println(); } } class Dato implements Comparable { int aa, m, d; Dato(int aa, int m, int d) { this.aa = aa; this.m = m; this.d = d; } @Override public int compareTo(Dato x) { if (aa != x.aa) return aa - x.aa; if (m != x.m) return m - x.m; return d - x.d; } @Override public String toString() { return aa + ":" + m + ":" + d; } } class BrukFrekvens { public static void main(String[] arg) { Frekvens mineData = new Frekvens(); mineData.add(new Dato(2000, 1, 1)); mineData.add(new Dato(2020, 5, 10)); mineData.add(new Dato(2020, 5, 10)); mineData.add(new Dato(2020, 5, 17)); mineData.add(new Dato(2020, 7, 17)); mineData.komprimer(); mineData.finnFlest(); System.out.println("Den vanligste datoen er " + mineData.hentFlest() + " (" + mineData.hentAntall() + " forekomster)."); } } // System.out.print("Data er "); mineData.visData();