I have two array of objects.
Array1 : [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}]
Array2 : [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}]
Here
array1 have classNames as A, B, C, and D.
array2 have classNames as X, Y, A, C, and Z
A function should return the classNames
of array2
such a way that the classNames
does not belong to array1
The return from the function will be an array which contains X, Y, and Z as elements.
How to write this function in javascript
with less time plexity, because array1 and array2 may have more than 20 objects.
Edit
This is the script which I used using for loop
array1 = [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}]
array2 = [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}]
function findSuggest(){
var sug = [];
for(array2_count=0;array2_count < array2.length;array2_count++){
for(array1_count=0;array1_count < array1.length;array1_count++){
if(array2[array2_count].className == array1[array1_count].className){
break;
}
else{
if(array1_count == (array1.length - 1)){
sug[sug.length] = array2[array2_count].className;
}
}
}
}
}
Here sug[]
will have all suggestions.
I have two array of objects.
Array1 : [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}]
Array2 : [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}]
Here
array1 have classNames as A, B, C, and D.
array2 have classNames as X, Y, A, C, and Z
A function should return the classNames
of array2
such a way that the classNames
does not belong to array1
The return from the function will be an array which contains X, Y, and Z as elements.
How to write this function in javascript
with less time plexity, because array1 and array2 may have more than 20 objects.
Edit
This is the script which I used using for loop
array1 = [{"id":20,"stName":"ABC","className":"A"},{"id":30,"stName":"ABD","className":"B"},{"id":40,"stName":"ABE","className":"C"},{"id":50,"stName":"ABF","className":"D"}]
array2 = [{"id":110,"stName":"ASA","className":"X"},{"id":120,"stName":"ASB","className":"Y"},{"id":130,"stName":"ASC","className":"A"},{"id":140,"stName":"ASD","className":"C"},{"id":150,"stName":"ASE","className":"Z"}]
function findSuggest(){
var sug = [];
for(array2_count=0;array2_count < array2.length;array2_count++){
for(array1_count=0;array1_count < array1.length;array1_count++){
if(array2[array2_count].className == array1[array1_count].className){
break;
}
else{
if(array1_count == (array1.length - 1)){
sug[sug.length] = array2[array2_count].className;
}
}
}
}
}
Here sug[]
will have all suggestions.
- 1 Do you have anything beyond the task description? This sounds like a homework assignment and you want someone to do it for you. – Tomalak Commented Jul 2, 2013 at 14:38
-
This is a part of a product am working on, and the data which I provided is just for simplify the question. This snippet a part of an algorithm, which need more efficiency. I already implemented this with normal
for
loop, but want to improve the efficiency of it. – Javad Shareef Commented Jul 2, 2013 at 14:42 - @lucuma I tried this by iterating to every element and pare its values. It is more time consuming if more number of objects are there in an array. – Javad Shareef Commented Jul 2, 2013 at 14:44
- 1 @JavadShareef Update your question with the code you've tried that you need help with. – lucuma Commented Jul 2, 2013 at 14:46
1 Answer
Reset to default 18Here's the general idea of the algorithm:
- Iterate over
Array1
.- Add the current item's
className
to a truth map
- Add the current item's
- Iterate over
Array2
- If the current item's
className
is not in the truth map, add it to the result.
- If the current item's
That simple, O(n+m)
(worst case O(2n)
). By a truth map, my intention is a plain js object, where each key is (in this case) a className
, and each value is true
.