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. int[] arr1 = { 1, 3, 9, 5 };
  13. int[] arr2 = { 7, 0, 5, 4, 3 };
  14. int arr1Len = arr1.length;
  15. int arr2Len = arr2.length;
  16. int[] both = new int[arr1Len+arr2Len];
  17. int[] result = new int[arr1Len+arr2Len];
  18. System.arraycopy(arr1, 0, both, 0, arr1Len);
  19. System.arraycopy(arr2, 0, both, arr1Len, arr2Len);
  20.  
  21. //Array sort complexity O(n) < x < O(n lg n)
  22. Arrays.sort(both);
  23.  
  24. //Sorted array duplication removal O(n)
  25. int counterUnique = 0;
  26. result[0] = both[0];
  27. for (int item : both) {
  28. if(result[counterUnique] != item){
  29. result[++counterUnique]=item;
  30. }
  31. }
  32.  
  33. //optional
  34. int[] rightSizeResult = Arrays.copyOf(result, counterUnique+1);
  35. System.out.println(Arrays.toString(rightSizeResult));
  36. }
  37. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
[0, 1, 3, 4, 5, 7, 9]