VMWare Inc Interview Question
Quality Assurance EngineersTeam: QATeam
Country: India
Interview Type: In-Person
If you cld use collections, then - Put all elements from both arrays in set, if it returns false, then add it to ur array. (Set returns false, if the object already present in it). Or u cld use hash map, put same key and value as ur input to map. If the return value is not null, then add it in new array.
import java.util.*;
class GetList
{
public static void main(String[] args)
{
Character arr1[]={'A','A','B','L','C','F','D','E','F','D','Z','R'};
Character arr2[]={'C','E','X','Z','M','X','P','L','M','N','S','T','P','B','A'};
List<Character> l1=new ArrayList<Character>();
List<Character> l2=new ArrayList<Character>();
l1=Arrays.asList(arr1);
l2=Arrays.asList(arr2);
System.out.println(method1(l1,l2));
}
static ArrayList<Character> method1(List<Character> a1,List<Character> a2)
{
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
for ( char a : a1 )
{
if(hm.containsKey(a))
{
int x=hm.get(a);
x++;
hm.put(a,x);
}
else
{
hm.put(a,1);
}
}
for(char b:a2)
{
if(hm.containsKey(b))
{
int x=hm.get(b);
x++;
hm.put(b,x);
}
else
{
hm.put(b,1);
}
}
ArrayList<Character> nal=new ArrayList<Character>();
Set<Character> s=hm.keySet();
for(char c : s)
{
if(hm.get(c)==2)
nal.add(c);
}
return nal;
}
}
public static ArrayList<String> findTwiceRepeat(List list1 , List list2){
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> input = new ArrayList<String>();
//contains the element which appears more than twice
ArrayList<String> more = new ArrayList<String>();
input.addAll(list1);
input.addAll(list2);
System.out.println("input : " + input);
for(int i=0;i < input.size();i++){
int count=1;
//if the element has already appeared twice, just go to next element
if(more.contains(input.get(i))){
continue;
}
for(int j=i+1;j<input.size();j++){
if(input.get(i).equals(input.get(j))){
count++;
}
// if the element appears more than twice , put it in the list
if(count > 2){
more.add(input.get(i));
break;
}
}
if(count == 2){
result.add(input.get(i));
}
}
return result;
}
Create a HashMap: where
<key> = element of the arraylist <AL1,AL2>,
<value> = number of times it appears in <AL1,AL2>
Once the HashMap is created, just restore the <keys> having <value>=2.
This logic will use extra space of O(N) = sizeof(AL1)+sizeof(AL2) in the worst case.
Time complexity will be order of 'N' .
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ExactTwo {
/**
* @param args
*/
public static ArrayList<Character> exactTwoChar(ArrayList<Character> AL1,ArrayList<Character> AL2)
{
ArrayList<Character> AL3 = new ArrayList<Character>();
ArrayList<Character> AL4 = new ArrayList<Character>();
ArrayList<Character> AL5 = new ArrayList<Character>();
AL3.addAll(AL1);
AL3.addAll(AL2);
Set<Character> s = new HashSet<Character>();
s.addAll(AL3);
AL4.addAll(s);
for (int i=0;i<AL4.size();i++)
{
int count = 0;
Character a = (char) AL4.get(i);
for (int j=0;j<AL3.size();j++)
{
if(a.equals(AL3.get(j)))
{
count++;
}
}
if(count == 2)
{
AL5.add(a);
}
}
return AL5;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Character> AL1 =
new ArrayList<Character>(Arrays.asList('A','A','B','L','C','F','D','E','F','D','Z','R'));
ArrayList<Character> AL2 =
new ArrayList<Character>(Arrays.asList('C','E','X','Z','M','X','P','L','M','N','S','T','P','B','A'));
ArrayList<Character> AL6 = new ArrayList<Character>();
AL6 = exactTwoChar(AL1,AL2);
for (Character ch : AL6)
{
System.out.println(ch);
}
}
}
public class GetPairElements {
private static List<String> getPairElements(List<String> a1, List<String> a2) {
List<String> all = new ArrayList<String>(a1);
all.addAll(a2);
Map<String, Integer> cMap = new HashMap<String, Integer>();
for(String s : all) {
Integer i = cMap.get(s);
if(i == null) {
cMap.put(s, 1);
} else {
cMap.put(s, ++i);
}
}
List<String> result = new ArrayList<String>();
Set<Entry<String, Integer>> entrySet= cMap.entrySet();
for(Entry<String, Integer> entry: entrySet) {
if(entry.getValue() == 2) {
result.add(entry.getKey());
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
String AL1[] = {"A","A","B","L","C","F","D","E","F","D","Z","R"};
String AL2[] = {"C","E","X","Z","M","X","P","L","M","N","S","T","P","B","A"};
List<String> pairElements = getPairElements(Arrays.asList(AL1), Arrays.asList(AL2));
System.out.println(pairElements);
}
}
private static void getCharCount(
HashMap<String, Integer> charCounter, ArrayList AL){
for(int i = 0; i < AL.size(); i++) {
String keyString = (String)AL.get(i);
if(charCounter.get(keyString) == null) {
charCounter.put(keyString, 1);
}
else {
int count = charCounter.get(keyString);
charCounter.put(keyString, ++count);
}
}
}
private static ArrayList twoCount(ArrayList AL1, ArrayList AL2){
HashMap<String, Integer> charCounter = new HashMap<>();
ArrayList AL3 = new ArrayList();
getCharCount(charCounter,AL1);
getCharCount(charCounter,AL2);
for(String s:charCounter.keySet()){
if(charCounter.get(s) == 2) {
AL3.add(s);
}
}
return AL3;
}
If its allowed to use extra space, create a int array of 256.
- KnowledgeSeeker August 06, 2013iterate through set 1, and increment value of array element at position (int)char. Then iterate through set 2.
Iterate through int array for all values = 2.