idzik

Java-BootCamp-Tablice-Zadanie12

Nov 20th, 2016
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class TabliceZadanie12 {
  4.  
  5.     public static int solution(int[] tab) {
  6.         HashMap<Integer, Integer> mapa = new HashMap<Integer, Integer>();
  7.         int result = 0;
  8.         for (int i = 0; i < tab.length; i++) {
  9.             if (mapa.containsKey(tab[i]))
  10.                 mapa.replace(tab[i], mapa.get(tab[i]) + 1);
  11.             else
  12.                 mapa.put(tab[i], 1);
  13.         }
  14.         for (Integer klucz : mapa.keySet()) {
  15.             Integer value = mapa.get(klucz);
  16.             if (value == 1)
  17.                 result = klucz;
  18.         }
  19.         return result;
  20.     }
  21.  
  22.     public static void main(String[] args) {
  23.         int[] tablica = { 4, 3, 4, 8, 3, 5, 5 };
  24.         System.out.println(solution(tablica));
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment