|
| 1 | +package arrayPrograms; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +public class ArrayPrograms { |
| 7 | + // Find leaders in an array |
| 8 | + public static void findLeaders(int[] arr) { |
| 9 | + if (arr.length == 0) { |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + int length = arr.length; |
| 14 | + |
| 15 | + if (length == 1) { |
| 16 | + System.out.print(arr[0]); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + int max = arr[length - 1]; |
| 21 | + System.out.print(max + " "); |
| 22 | + |
| 23 | + for (int i = length - 2; i >= 0; i--) { |
| 24 | + if (arr[i] > max) { |
| 25 | + System.out.print(arr[i] + " "); |
| 26 | + max = arr[i]; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Find intersection of two arrays |
| 32 | + public static void findIntersection(int[] arr1, int[] arr2) { |
| 33 | + Set<Integer> set1 = new HashSet<>(); |
| 34 | + Set<Integer> set2 = new HashSet<>(); |
| 35 | + for (int e : arr1) { |
| 36 | + set1.add(e); |
| 37 | + } |
| 38 | + for (int e : arr2) { |
| 39 | + set2.add(e); |
| 40 | + } |
| 41 | + |
| 42 | + set1.retainAll(set2); |
| 43 | + |
| 44 | + System.out.println(set1); |
| 45 | + } |
| 46 | + |
| 47 | + // Find intersection of two arrays |
| 48 | + public static void findIntersection1(int[] arr1, int[] arr2) { |
| 49 | + Set<Integer> set1 = new HashSet<>(); |
| 50 | + for (int e : arr1) { |
| 51 | + set1.add(e); |
| 52 | + } |
| 53 | + |
| 54 | + // Using contains |
| 55 | + for (int e : arr2) { |
| 56 | + if (set1.contains(e)) { |
| 57 | + System.out.print(e + " "); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Using add |
| 62 | + /*for (int e : arr2) { |
| 63 | + if (!set1.add(e)) { |
| 64 | + System.out.print(e + " "); |
| 65 | + } |
| 66 | + }*/ |
| 67 | + } |
| 68 | + |
| 69 | + public static String[] sortArrayAscending(String[] arr) { |
| 70 | + int length = arr.length; |
| 71 | + |
| 72 | + String temp; |
| 73 | + for (int i = 0; i < length; i++) { |
| 74 | + for (int j = i + 1; j < length; j++) { |
| 75 | + if (arr[i].compareToIgnoreCase(arr[j]) > 0) { |
| 76 | + temp = arr[i]; |
| 77 | + arr[i] = arr[j]; |
| 78 | + arr[j] = temp; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return arr; |
| 84 | + } |
| 85 | + |
| 86 | + public static String[] sortArrayDescending(String[] arr) { |
| 87 | + int length = arr.length; |
| 88 | + |
| 89 | + String temp; |
| 90 | + for (int i = 0; i < length; i++) { |
| 91 | + for (int j = i + 1; j < length; j++) { |
| 92 | + if (arr[i].compareToIgnoreCase(arr[j]) < 0) { |
| 93 | + temp = arr[i]; |
| 94 | + arr[i] = arr[j]; |
| 95 | + arr[j] = temp; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return arr; |
| 101 | + } |
| 102 | + |
| 103 | + public static int[] getOddNumbers(int[] arr) { |
| 104 | + int oddNoCount = 0; |
| 105 | + for (int i = 0; i < arr.length; i++) { |
| 106 | + if (arr[i] % 2 != 0) { |
| 107 | + oddNoCount++; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + int[] result = new int[oddNoCount]; |
| 112 | + int resultIndex = 0; |
| 113 | + |
| 114 | + for (int i = 0; i < arr.length; i++) { |
| 115 | + if (arr[i] % 2 != 0) { |
| 116 | + result[resultIndex] = arr[i]; |
| 117 | + resultIndex += 1; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + public static int[] removeArrayElement(int[] arr, int element) { |
| 125 | + int length = arr.length; |
| 126 | + int[] newArr = new int[length - 1]; |
| 127 | + |
| 128 | + int newArrIndex = 0; |
| 129 | + for (int j : arr) { |
| 130 | + if (j != element) { |
| 131 | + newArr[newArrIndex++] = j; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return newArr; |
| 136 | + } |
| 137 | +} |
0 commit comments