Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public static int[] removeDuplicates(int[] input) {
  2. int arrayLength = input.length;
  3. Integer[] nums = new Integer[arrayLength];
  4. for (int el : input) {
  5. if (!doesElementExistInArray(el, nums)) {
  6. addToArray(el, nums);
  7. }
  8. }
  9. int count = 0;
  10.  
  11. for(int i = 0; i < nums.length; i++) {
  12. if(nums[i] == null)
  13. continue;
  14. count++;
  15. }
  16. int[] array = new int[count];
  17. for(int i = 0; i < count; i++) {
  18. array[i] = nums[i];
  19. }
  20.  
  21. return array;
  22. }
  23. public static boolean doesElementExistInArray(int num, Integer[] arr){
  24. for (int i = 0; i < arr.length; i++) {
  25. if (arr[i] != null)
  26. if(arr[i] == num)
  27. return true;
  28. }
  29. return false;
  30. }
  31. public static Integer[] addToArray(int num, Integer[] arr){
  32. for (int i = 0; i < arr.length; i++){
  33. if (arr[i] != null)
  34. continue;
  35. arr[i] = num;
  36. return arr;
  37. }
  38. return arr;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement