Closed
Description
https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms
not too good
https://github.com/loiane/javascript-datastructures-algorithms/blob/master/chapter06/01-Set.js
https://github.com/loiane/javascript-datastructures-algorithms/blob/master/chapter06/01-Set2.js
this.intersection = function(otherSet){
var intersectionSet = new Set(); //{1}
var values = this.values();
for (var i=0; i<values.length; i++){ //{2}
if (otherSet.has(values[i])){ //{3}
intersectionSet.add(values[i]); //{4}
}
}
return intersectionSet;
};
this.intersection = function(otherSet){
let intersectionSet = new Set(); //{1}
let values = this.values();
for (let i=0; i<values.length; i++){ //{2}
if (otherSet.has(values[i])){ //{3}
intersectionSet.add(values[i]); //{4}
}
}
return intersectionSet;
};
intersection(otherSet){
let intersectionSet = new Set();
let values = this.values();
for (let i=0; i<values.length; i++){
if (otherSet.has(values[i])){
intersectionSet.add(values[i]);
}
}
return intersectionSet;
}
good
this.intersection = (otherSet) => {
let intersectionSet = new Set();
// self
let values = this.values();
let othervalues = otherSet.values();
let small_size = ((othervalues.length - values.length) > 0 ? values : othervalues);
// small & reduce iterate times
for (var i = 0; i < small_size.length; i++) {
if (small_size.has(values[i])) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
};
javascript-datastructures-algorithms
https://github.com/loiane/javascript-datastructures-algorithms
https://github.com/gildata/RAIO/issues/167#issuecomment-330818729