// Write a function that takes two arrays as input, // each array contains a list of A-Z; // Your program should return True if the 2nd array is a subset of 1st array, or False if not. function isSubset(source, subset) { let isShared = true; // Create an object which will be used for store formatted subset object // in order to reduce the duplicated string for a linear search later let sourceObject = {}; let subsetObject = {}; for (let i = 0; i < source.length; i++) { sourceObject[source[i]] = 1; } for (let i = 0; i < subset.length; i++) { subsetObject[subset[i]] = 1; } // Linear search for finding subset string for (let key in subsetObject) { if (!(key in sourceObject)) { isShared = false; } } return isShared; } console.log(isSubset(['A','B','C','D','E'], ['A','E','D'])); // true console.log(isSubset(['A','B','C','D','E'], ['A','D','Z'])); // false console.log(isSubset(['A','D','E'], ['A','A','D','E'])); // true // Big-O notation explain // The minumum amount of loop for this function is (source.length * subset.length) + (source.length + subset.length) // ex. isSubset(['A','B','C','D','E'], ['A','D','Z']) = (5 * 3) + (5 + 3) = 23 times // if we increase input size from (3, 3) into (6, 6) the result time cosuming will be n ^ 2 // That is mean for Big-O notation,we got O(n ^ 2) (Because the new sizing is ^2 from the original sizing)