Created
February 6, 2020 00:50
-
-
Save runandrerun/8f12ced54883a8171acbe77b6fc26385 to your computer and use it in GitHub Desktop.
spotUniqueCharSet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const paragraph = "If you want to jumpstart the process of talking to us about this role, here’s a little challenge: write a program that outputs the largest unique set of characters that can be removed from this paragraph without letting its length drop below 50."; | |
const spotUniqueCharSet = (paragraph) => { | |
const dictionary = {}; | |
const sortedDictionary = {}; | |
let characterSet = []; | |
let sortable = []; | |
let paragraphLength = paragraph.length; | |
// map over paragraph to find unique appearance counts in the paragraph provided | |
for (let i = 0; i < paragraph.length; i++) { | |
if (!dictionary[paragraph[i]]) { | |
dictionary[paragraph[i]] = 1; | |
} else { | |
dictionary[paragraph[i]]++ | |
}; | |
}; | |
// end seek times appeared section | |
// sort the dictionary object by transposing to an array and then back to a new sorted dictionary | |
for (let char in dictionary) { | |
sortable.push([char, dictionary[char]]); | |
}; | |
sortable.sort((a, b) => { | |
return a[1] - b[1]; | |
}); | |
sortable.forEach(charValPair => { | |
sortedDictionary[charValPair[0]] = charValPair[1]; | |
}); | |
// end sorting section | |
// loop through sorted dictionary in order to find the largest unique set of characters that can be removed without lowering the paragraph past 50 | |
for (let char in sortedDictionary) { | |
if (paragraphLength - sortedDictionary[char] > 50) { | |
paragraphLength -= sortedDictionary[char]; | |
characterSet.push(char); | |
}; | |
}; | |
// end characterSet retrieval section | |
console.log(characterSet); | |
return characterSet; | |
}; | |
spotUniqueCharSet(paragraph); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment