I want to see if an object with an equal value is in the container in logarithmic time.
I would like to have the following functions:
const a = [];
const el1 = {name: 'name1', directive: 'directive1'};
const el2 = {name: 'name2', directive: 'directive2'};
const el3 = {name: 'name3', directive: 'directive3'};
const b = {name: 'name1', directive: 'directive1'};
a.push(el1);
a.push(el2);
a.push(el3);
if(a.some(el => (el.name === b.name && el.directive === b.directive ))) {
console.log("YES!");
} else {
console.log("NO!");
}
This gives me the result I want. However, this time is O (N).
const s = new Set();
const el1 = {name: 'name1', directive: 'directive1'};
const el2 = {name: 'name2', directive: 'directive2'};
const el3 = {name: 'name3', directive: 'directive3'};
const b = {name: 'name1', directive: 'directive1'};
s.add(el1);
s.add(el2);
s.add(el3);
if(s.has(b)) {
console.log("YES!");
} else {
console.log("NO!");
}
This is O (logN), but the result is not the one I want.
So, what kind of Javascript structure can I use to print YESin the above code and have O (logN) complexity? (I do not want to implement the data structure myself)
source
share