pawanKjajara
BAN USERAs time complexity is not mentioned with question, here is an easy solution:
public static void removeDuplicates(int[] arr1, int[] arr2){
int[] arr = new int[arr1.length + arr2.length];
int index=0,i,j;
for (i=0;i<arr1.length;i++){
if(!contains(arr, arr1[i])){
arr[index++]=arr1[i];
}
}
for (j = 0; j < arr2.length; j++) {
if (!contains(arr, arr2[j])) {
arr[index++] = arr2[j];
}
}
for (int a: arr)
System.out.println(a);
}
private static boolean contains(int[] arr, int i) {
for (int a: arr){
if(a==i) return true;
}
return false;
}
package edu.sjsu.java.string;
import java.util.Arrays;
public class AnagramTest {
public static boolean isAnagram(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
}
if (s1.length() != s2.length()) {
return false;
}
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
String sc1 = new String(c1);
String sc2 = new String(c2);
return sc1.equals(sc2);
}
}
Thanks for solution Charlie. Here is code with test data:
public class MyHashMap<K, T> {
public static void main(String[] args) {
MyHashMap<String, String> map = new MyHashMap<String, String>();
map.put("CA", "California");
map.put("TX", "Texas");
map.put("TX", "Texas1");
map.put("TX", "Texas2");
map.put("GA", "Georgia");
String s = map.get("CA");
System.out.println(map.get("CA"));
System.out.println(map.get("TX"));
System.out.println(map.get("GA"));
}
static class Pair<K, T> {
K key;
T value;
Pair(K k, T v) {
key = k;
value = v;
}
}
int size = 10;
LinkedList buckets[] = new LinkedList[size];
int hash(K key) {
return key.hashCode() % size;
}
void put(K key, T value) {
int index = hash(key);
if (buckets[index] == null) {
buckets[index] = new LinkedList();
}
for (Object p : buckets[index]) {
Pair<K, T> pair = (Pair<K, T>) p;
if (pair.key.equals(key)) {
pair.value = value;
return;
}
}
buckets[index].add(new Pair(key, value));
}
T get(K key) {
int index = hash(key);
if (buckets[index] == null)
return null;
for (Object p : buckets[index]) {
Pair<K, T> pair = (Pair<K, T>) p;
if (pair.key.equals(key))
return pair.value;
}
return null;
}
}
- pawanKjajara February 14, 2014Your code doesn't cover the case of instance!=null. Just adding for other users.
- pawanKjajara February 14, 2014Is it a great idea to use inbuilt functions when asked to solve this problem in an interview? What impression does it creates if you are using inbuilt functions and not using your own functions.
- pawanKjajara October 11, 2011
Your function is simply printing duplicate elements but whats really needed here is a third array holding unique elements from both arrays passed by caller.
- pawanKjajara February 24, 2014