Files
ASDL/asdl/src/parziale/p231110/Esercizio.java
T
2026-05-19 13:42:21 +02:00

36 lines
971 B
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package parziale.p231110;
import java.util.Map;
import java.util.HashMap;
public class Esercizio {
/*
* Scrivere un metodo generico statico che prende in input
* un array di elementi e restituisce il
* numero di oggetti presenti allinterno dellarray che
* non presentano duplicazioni. Quindi
* utilizzare il metodo sul tipo Integer.
* Esempio: se larray fosse {10, 6, 5, 6, 7, 8, 6, 8, 10, 12, 10},
* il metodo dovrebbe restituire
* lintero 3 (in grassetto i 3 elementi che non presentano duplicazioni).
*/
public static int numeroOccorrenzeSingole(int[] array) {
if (array == null) throw new NullPointerException();
Map<Integer, Integer> lista = new HashMap<Integer, Integer>();
for (Integer i : array) {
if (lista.get(i) == null) lista.put(i, 1);
else lista.put(i, lista.get(i) + 1);
}
int counter = 0;
for (Integer i : lista.keySet()) {
if (lista.get(i) == 1) counter++;
}
return counter;
}
}