fork download
  1. //brue-force
  2. //check if a2 is a subset of a1 when a2 has no duplicates.
  3. //check if a2 is a subset of a1 when a2 has duplicates.
  4. import java.util.*;
  5. class Ideone {
  6. public static void main(String[] args) throws java.lang.Exception {
  7. int[] a1 = {1, 2, 3, 3, 3, 3, 4, 1, 5, 2, 7, 6};
  8. int[] a2 = {1, 6, 3, 2, 2}; // has duplicates
  9.  
  10. int[] visited = new int[a1.length];
  11.  
  12. boolean isSubset = true;
  13.  
  14. for (int i = 0; i < a2.length; i++) {
  15. boolean found = false;
  16. for (int j = 0; j < a1.length; j++) {
  17. if (a2[i] == a1[j] && visited[j] == 0) {
  18. found = true;
  19. visited[j] = 1;
  20. break;
  21. }
  22. }
  23. if (!found) {
  24. isSubset = false;
  25. break;
  26. }
  27. }
  28.  
  29. if (isSubset) {
  30. System.out.println("is a subset");
  31. } else {
  32. System.out.println("is not a subset");
  33. }
  34. }
  35. }
  36.  
Success #stdin #stdout 0.07s 54676KB
stdin
Standard input is empty
stdout
is a subset