JavaScript Array to Set

Just pass the array to the Set constructor. The Set constructor accepts an iterable parameter. The Array object implements the iterable protocol, so its a valid parameter. var arr = [55, 44, 65]; var set = new Set(arr); console.log(set.size === arr.length); console.log(set.has(65)); See here

Swift Set to Array

You can create an array with all elements from a given Swift Set simply with let array = Array(someSet) This works because Set conforms to the SequenceType protocol and an Array can be initialized with a sequence. Example: let mySet = Set([“a”, “b”, “a”]) // Set<String> let myArray = Array(mySet) // Array<String> print(myArray) // [b, … Read more

How to customize object equality for JavaScript Set

Update 3/2022 There is currently a proposal to add Records and Tuples (basically immutable Objects and Arrays) to Javascript. In that proposal, it offers direct comparison of Records and Tuples using === or !== where it compares values, not just object references AND relevant to this answer both Set and Map objects would use the … Read more

How to “perfectly” override a dict?

You can write an object that behaves like a dict quite easily with ABCs (Abstract Base Classes) from the collections.abc module. It even tells you if you missed a method, so below is the minimal version that shuts the ABC up. from collections.abc import MutableMapping class TransformedDict(MutableMapping): “””A dictionary that applies an arbitrary key-altering function … Read more

How to calculate the intersection of two sets? [duplicate]

Use the retainAll() method of Set: Set<String> s1; Set<String> s2; s1.retainAll(s2); // s1 now contains only elements in both sets If you want to preserve the sets, create a new set to hold the intersection: Set<String> intersection = new HashSet<String>(s1); // use the copy constructor intersection.retainAll(s2); The javadoc of retainAll() says it’s exactly what you … Read more