fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int arr1[] = {2,4,2,2,1,1};
  14. int arr2[] = {1,1,2,2,4,9};
  15.  
  16. Map<Integer,Integer> map = new HashMap<>();
  17. //Inserting all the elements along with their frequencies into the hashmap of arr1
  18. for(int i = 0;i<arr1.length;i++){
  19. map.put(arr1[i],map.getOrDefault(arr1[i],0) + 1);
  20. }
  21.  
  22. //Check if map has the same or greater frequency of numbers
  23. for(int i=0;i<arr2.length;i++){
  24. if(!map.containsKey(arr2[i])){
  25. System.out.println("Array2 is not a subset of Array1");
  26. return;
  27. }
  28. if(map.get(arr2[i]) == 0){
  29. System.out.println("Array2 is not a subset of Array1");
  30. return;
  31. }
  32.  
  33. int countOfElement = map.get(arr2[i]);
  34. map.put(arr2[i],countOfElement -1);
  35. }
  36.  
  37. System.out.println("Arr2 is a subset of Arr1");
  38. }
  39. }
Success #stdin #stdout 0.09s 54612KB
stdin
Standard input is empty
stdout
Array2 is not a subset of Array1