Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kayla - 🌊 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions lib/exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,56 @@
// Time Complexity: ?
// Space Complexity: ?
function grouped_anagrams(strings) {
throw new Error("Method hasn't been implemented yet!");

if (!strings.length) {
return []
}

let anagrams = {}

for (const string of strings) {
const sortedString = string.split("").sort().join("");
// console.log(sortedString)

if(anagrams[sortedString]) {
// console.log(anagrams[sortedString])
anagrams[sortedString].push(string)

}else {
anagrams[sortedString] = [string];
}
}
return Object.values(anagrams)




}

// This method will return the k most common elements
// in the case of a tie it will select the first occuring element.
// Time Complexity: ?
// Space Complexity: ?
function top_k_frequent_elements(list, k) {
throw new Error("Method hasn't been implemented yet!");
if (!list.length) return []

let numbersCount = {}

for (const num of list) {
numbersCount[num] = numbersCount[num] ? numbersCount[num] + 1 : 1;
}
console.log(numbersCount)

let topFrequentElements = []

const valuesList = Object.keys(numbersCount).sort((num1, num2) => numbersCount[num2] - numbersCount[num1]);

for (let i = 0; i < k; i++) {
topFrequentElements.push(parseInt(valuesList[i]))
}

return topFrequentElements

}


Expand Down