Use arrays, strings, maps, sets and sorting in Python, Java, C++ and JavaScript side by side.
Every language has the same four tools, so write the same steps in each and check where it differs.
Use it when a task counts, groups, looks up or orders values. Signal words: how often, unique, first seen, sort by.
Start: pick the tool: a map to count or look up, a set to test membership, a sort to order.
Step: read the list once, updating the map or set, or sort a copy with a key.
Stop: compare the entries with a tie rule that does not depend on map order.
It does not change the class of the task. It makes the first pass cost n instead of n squared.
Cost: a map reads the list once, O(n). A sort costs O(n log n).
def top_word(words):
# [1] A table from each word to how many times it appeared.
counts = {}
for word in words:
# [2] Count the word, starting from zero for a new one.
counts[word] = counts.get(word, 0) + 1
best_word = ""
best_count = 0
for word in counts:
# [3] Keep the higher count; on a tie keep the alphabetically smaller word.
if counts[word] > best_count or (counts[word] == best_count and word < best_word):
best_word = word
best_count = counts[word]
# [4] Return the top word, or the empty string for an empty list.
return best_wordpublic String topWord(String[] words) {
// [1] A table from each word to how many times it appeared.
java.util.HashMap<String, Integer> counts = new java.util.HashMap<>();
for (String word : words) {
// [2] Count the word, starting from zero for a new one.
counts.put(word, counts.getOrDefault(word, 0) + 1);
}
String bestWord = "";
int bestCount = 0;
for (java.util.Map.Entry<String, Integer> entry : counts.entrySet()) {
String word = entry.getKey();
int count = entry.getValue();
// [3] Keep the higher count; on a tie keep the alphabetically smaller word.
if (count > bestCount || (count == bestCount && word.compareTo(bestWord) < 0)) {
bestWord = word;
bestCount = count;
}
}
// [4] Return the top word, or the empty string for an empty list.
return bestWord;
}#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
string topWord(const vector<string>& words) {
// [1] A table from each word to how many times it appeared.
unordered_map<string, int> counts;
for (const string& word : words) {
// [2] Count the word, starting from zero for a new one.
counts[word]++;
}
string bestWord = "";
int bestCount = 0;
for (const auto& entry : counts) {
const string& word = entry.first;
int count = entry.second;
// [3] Keep the higher count; on a tie keep the alphabetically smaller word.
if (count > bestCount || (count == bestCount && word < bestWord)) {
bestWord = word;
bestCount = count;
}
}
// [4] Return the top word, or the empty string for an empty list.
return bestWord;
}function topWord(words) {
// [1] A table from each word to how many times it appeared.
const counts = new Map();
for (const word of words) {
// [2] Count the word, starting from zero for a new one.
counts.set(word, (counts.get(word) ?? 0) + 1);
}
let bestWord = "";
let bestCount = 0;
for (const [word, count] of counts) {
// [3] Keep the higher count; on a tie keep the alphabetically smaller word.
if (count > bestCount || (count === bestCount && word < bestWord)) {
bestWord = word;
bestCount = count;
}
}
// [4] Return the top word, or the empty string for an empty list.
return bestWord;
}def order_by_length_then_text(words):
# [1] Sort a copy, so the caller's list stays as it was.
ordered = list(words)
# [2] The key puts the length first, then the text.
ordered.sort(key=lambda word: (len(word), word))
# [3] Return the ordered words.
return orderedpublic String[] orderByLengthThenText(String[] words) {
// [1] Sort a copy, so the caller's list stays as it was.
String[] ordered = words.clone();
// [2] The key puts the length first, then the text.
java.util.Arrays.sort(ordered, (a, b) -> a.length() != b.length() ? Integer.compare(a.length(), b.length()) : a.compareTo(b));
// [3] Return the ordered words.
return ordered;
}#include <algorithm>
#include <string>
#include <vector>
using namespace std;
vector<string> orderByLengthThenText(const vector<string>& words) {
// [1] Sort a copy, so the caller's list stays as it was.
vector<string> ordered = words;
// [2] The key puts the length first, then the text.
sort(ordered.begin(), ordered.end(), [](const string& a, const string& b) {
if (a.size() != b.size()) {
return a.size() < b.size();
}
return a < b;
});
// [3] Return the ordered words.
return ordered;
}function orderByLengthThenText(words) {
// [1] Sort a copy, so the caller's list stays as it was.
const ordered = [...words];
// localeCompare is avoided below because its result depends on the locale.
// [2] The key puts the length first, then the text.
ordered.sort((a, b) => (a.length !== b.length ? a.length - b.length : a < b ? -1 : a > b ? 1 : 0));
// [3] Return the ordered words.
return ordered;
}This version sorts numbers with list.sort() and no comparator.
This version returns the first word it meets with the highest count, and relies on the map's order for ties.
This version builds a 3 by 3 grid with grid = [[0] * 3] * 3.
This version sorts C++ records by one key and expects equal keys to stay in input order.
This Java version sorts an Integer[] with the comparator (a, b) -> a - b.
This Java version reads two counts from a HashMap<String, Integer> and compares them with ==.
A task asks for the words in alphabetical order with their counts. A hash map does not keep order, so counting is only half the job.
systemdesignmastery.dev/dsa/cheatsheets/language-toolkit