Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Vishal1297/e0763726503fb97680d7950387392486 to your computer and use it in GitHub Desktop.
Save Vishal1297/e0763726503fb97680d7950387392486 to your computer and use it in GitHub Desktop.
To be fixed
import java.util.Objects;
public class Solution {
public static void main(String[] args) {
int[] arr1 = {1, 20, 3, 4, 2};
int[] arr2 = {1, 70, 900, 445, 5};
int[] result = mergeWithOutDuplicates(arr1, arr2);
for (int i : result) {
if (i != 0) {
System.out.print(i + "\t");
}
}
// Output : 1 2 3 4 5 7 9
System.out.println();
}
private static int getIndex(int key, int capacity) {
int index = hashCode(key) % capacity;
return Math.abs(index);
}
public static int hashCode(int key) {
return Objects.hashCode(key);
}
public static int[] mergeWithOutDuplicates(int[] arr1, int[] arr2) {
int newLen = 100;
int[] result = new int[newLen];
removeDuplicates(arr1, result, newLen);
removeDuplicates(arr2, result, newLen);
return result;
}
public static void removeDuplicates(int[] arr, int[] result, int newLen) {
for (int i = 0; i < arr.length; i++) {
int index = getIndex(arr[i], newLen);
if (result[index] == 0) {
result[index] = arr[i];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment