Q: Count unique words in array
Solution 1
const input = ["you", "me", "them", "them", "me", "them"];
const getUniqueWordsInArray = (inputArray) => {
return [...new Set(inputArray)].length;
};
getUniqueWordsInArray(input); // 3
Big O -> O(n)
Solution 2
const input = ["you", "me", "them", "them", "me", "them"];
const getUniqueWordsInArray = (inputArray) => {
const resultsMap = {};
inputArray.forEach((word) => {
resultsMap[word] = true;
});
return Object.keys(resultsMap).length;
};
getUniqueWordsInArray(input); //3
Big O -> O(2n) -> O(n)