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 BrukFrekvens2 { 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)."); } } class BrukFrekvens { public static void main(String[] arg) { Frekvens mineData = new Frekvens<>(); mineData.add(new Person(120101)); mineData.add(new Person(130101)); mineData.add(new Person(130101)); mineData.add(new Person(221203)); mineData.add(new Person(250508)); mineData.komprimer(); mineData.finnFlest(); System.out.println("Den vanligste foedselsdatoen er " + mineData.hentFlest() + " (" + mineData.hentAntall() + " forekomster)."); } } class Person implements Comparable { int fdato; Person (int f) { fdato = f; } @Override public int compareTo (Person p) { if (fdato < p.fdato) return -1; if (fdato > p.fdato) return 1; return 0; } @Override public String toString () { return "" + fdato; } }