VMWare Inc Interview Question
Quality Assurance EngineersTeam: QATeam
Country: India
Interview Type: In-Person
You can use to ArrayList and
import java.util.*;
public class UniqueElements {
public static void main(String[] args) {
List <Integer> al1 = new ArrayList<Integer>();
List <Integer>al2 = new ArrayList<Integer>();
al1.add(1);
al1.add(4);
al1.add(5);
al1.add(9);
al1.add(10);
al2.add(2);
al2.add(3);
al1.add(3);
al2.add(5);
al2.add(6);
al2.add(7);
al2.add(10);
al2.add(11);
al2.add(6);
al2.add(9);
System.out.println(al1);
System.out.println(al2);
List <Integer>result = new ArrayList <Integer>();
int i,j;
for(i=0;i<al1.size();i++)
{
if(!result.contains(al1.get(i)))
{
result.add(al1.get(i));
}
}
for(j=0;j<al2.size();j++)
{
if(!result.contains(al2.get(j)))
{
result.add(al2.get(j));
}
}
System.out.println(result);
}
}
Output :
Contents of Array1 : [1, 4, 5, 9, 10, 3]
Contents of Array2 : [2, 3, 5, 6, 7, 10, 11, 6, 9]
Unique elements in Result : [1, 4, 5, 9, 10, 3, 2, 6, 7, 11]
void make_unique(vector<int> & first , vector<int> & second , vector<int> &third)
{
sort(first.begin(),first.end());
sort(second.begin(), second.end());
third.clear();
int f_pos = 0 , s_pos = 0 ; // index on first array and second array
while(f_pos < first.size() && s_pos <second.size())
{
// 2 elements to compare
if(first[f_pos] < second[s_pos])
{
third.push_back(first[f_pos]);
// i want to skip all element in first array which are the same as first[f_pos]
skip_dup(first,f_pos);
}
else if( second[s_pos] < first[f_pos])
{
third.push_back(second[s_pos]);
// // i want to skip all element in second array which are the same as second[f_pos]
skip_dup(second,s_pos);
}
else
{
// 2 elements quals so push any of them and skip dup in both
third.push_back(first[f_pos]);
skip_dup(first,f_pos);
skip_dup(second,s_pos);
}
}
while(f_pos <first.size())
{
third.push_back(first[f_pos]);
skip_dup(first,f_pos);
}
while(s_pos <second.size())
{
third.push_back(second[s_pos]);
skip_dup(second,s_pos);
}
}
Time O(NlogN) , Space O(1)
As 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;
}
#include <iostream>
#include <vector>
#include <set>
int main() {
std::vector<int> first = { 5, 4, 3, 2, 1, 6, 7, 8};
std::vector<int> second = { 5, 6, 7, 8, 9, 10, 11, 20, 19, 18};
std::set<int> rSet(&first[0], &first[0] + first.size());
rSet.insert(second.begin(), second.end());
std::vector<int> third(rSet.begin(), rSet.end());
for (size_t i = 0; i < third.size(); ++i) {
std::cout << third[i] << std::endl;
}
std::cin.get();
return 0;
}
O(nlogn).
public class PutUniqueDataFrom2ArraysIn3rdArray1
{
public static void main(String args[])
{
ArrayList ar = new ArrayList();
int[] a1 = new int[]{1,2,3,4,5};
int[] a2 = new int[]{1,3,5,7};
for(int i=0;i<a1.length;i++)
{
if(ar.contains(a1[i]) == false)
ar.add(a1[i]);
}
for(int i=0;i<a2.length;i++)
{
if(ar.contains(a2[i]) == false)
ar.add(a2[i]);
}
System.out.println("arraylist= "+ar);
}
}
#include<stdio.h>
#define MAX 10
#define IN 10
int main()
{
int array_first[MAX] = {1,2,3,4,5,6,7,8,9,10};
int array_second[IN] = {11,12,13,14,15,2,4,1,3,6};
int temp[MAX]= {0};
int i,j,k=0;
for (i=0 ;i<MAX;i++)
{
for (j=0;j<MAX;j++)
{
if (array_first[i] == array_second[j])
{
temp[k] = array_first[i];
printf("%d ",temp[k]);
k++;
}
}
}
return 0;
}
Sort both the arrays and use a modified version of the 'merge' step that checks for the uniqueness of an element in both the arrays.
- Murali Mohan August 06, 2013