mac->arch
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
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 all’interno dell’array che
|
||||
* non presentano duplicazioni. Quindi
|
||||
* utilizzare il metodo sul tipo Integer.
|
||||
* Esempio: se l’array fosse {10, 6, 5, 6, 7, 8, 6, 8, 10, 12, 10},
|
||||
* il metodo dovrebbe restituire
|
||||
* l’intero 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user