Question
Given k arrays assuming they're all length N , choose one array as a query array with which to check if its elements are common
Given k arrays assuming they're all length N, choose one array as a query array with which to check if its elements are common in all arrays. The return value should be an array whose elements are common between all input arrays. Develop a linear solution. Can this be done?
Pseudocode for a quadratic, not linear solution:
for each element X in Query_Collection {
for each other collection {
for each element Y in current_collection {
if X.compareTo(Y) == 0 {
X is common between Query_Collection and current_collection
break
}
}
if X not in current_collection {
X not common to all collections
}
}
if X exists in all collections {
add X to common_collection
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started