fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] strg) {
  5. int[][] arr = {{1, 1, 1, 1}, {4, 4, 4, 4}, {3, 3, 1, 1}};
  6. numOfColors(arr);
  7. }
  8. static int numOfColors(int[][] map) {
  9. ArrayList<Integer> intlist = new ArrayList<Integer>();
  10. for (int o = 0; o < map.length; o++) {
  11. for (int n = 0; n < map[o].length; n++) {
  12. intlist.add(map[o][n]);
  13. }
  14. }
  15. intlist = removeDuplicates(intlist, 0);
  16. System.out.println(intlist.size()+" " +intlist);
  17. return intlist.size();
  18. }
  19. static ArrayList<Integer> removeDuplicates(ArrayList<Integer> list, int counter) {
  20. if (list == null) {
  21. throw new NullPointerException();
  22. }
  23. if (counter < list.size()) {
  24. if (list.contains(list.get(counter))) {
  25. if (list.lastIndexOf(list.get(counter)) != counter) {
  26. list.remove(list.lastIndexOf(list.get(counter)));
  27. counter--;
  28. }
  29. }
  30. removeDuplicates(list, ++counter);
  31. }
  32. return list;
  33. }
  34. }
  35.  
  36.  
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
3 [1, 4, 3]