Sponsored
Sponsored
This approach utilizes hash maps (or dictionaries) to store the relationship between teachers and the unique subjects they teach. The usage of sets facilitates the storage of unique subject IDs for each teacher. After accumulating the subjects for each teacher, counting the number of unique subjects can easily be done by checking the length of each set.
This method will efficiently group, count, and return the results.
Time Complexity: O(n), where n is the number of records in the input table, assuming average O(1) time complexity for set operations.
Space Complexity: O(m), where m is the number of unique (teacher_id, subject_id) pairs.
1// JavaScript solution
2function countUniqueSubjects(teacherRecords) {
3 const teacherToSubjects = {};
4 teacherRecords.forEach(([teacherId, subjectId]) => {
5 if (!teacherToSubjects[teacherId]) {
6 teacherToSubjects[teacherId] = new Set();
7 }
8 teacherToSubjects[teacherId].add(subjectId);
9 });
10
11 const result = [];
12 for (const [teacherId, subjects] of Object.entries(teacherToSubjects)) {
13 result.push({ teacher_id: +teacherId, cnt: subjects.size });
14 }
15 return result;
16}
17
18// Example input
19const teacherRecords = [
20 [1, 2, 3],
21 [1, 2, 4],
22 [1, 3, 3],
23 [2, 1, 1],
24 [2, 2, 1],
25 [2, 3, 1],
26 [2, 4, 1]
27];
28
29// Function call
30console.log(countUniqueSubjects(teacherRecords));
This JavaScript solution uses objects to map each teacher ID to a Set that contains their unique subject IDs. Iterating through the input data, it populates the Set for each teacher. In the end, it calculates the size of each Set and formats the results accordingly.
This approach imitates SQL operations by using arrays or similar data structures to first group data by teacher and then reducing the grouped data to count the unique subjects for each group. This approach can use sorting and manual iteration to simulate a GROUP BY operation.
Time Complexity: O(n) due to single-pass processing and set operations.
Space Complexity: O(m) corresponding to unique (teacher_id, subject_id) pairs.
1// C++ solution
2#include <iostream>
3#include <vector>
4#include <unordered_map>
5#include <unordered_set>
std::vector<std::pair<int, int>> countUniqueSubjects(const std::vector<std::tuple<int, int, int>>& teacherRecords) {
std::unordered_map<int, std::unordered_set<int>> teacherToSubjects;
for (const auto& record : teacherRecords) {
int teacherId = std::get<0>(record);
int subjectId = std::get<1>(record);
teacherToSubjects[teacherId].insert(subjectId);
}
std::vector<std::pair<int, int>> result;
for (const auto& entry : teacherToSubjects) {
result.emplace_back(entry.first, entry.second.size());
}
return result;
}
int main() {
std::vector<std::tuple<int, int, int>> teacherRecords = {
{1, 2, 3},
{1, 2, 4},
{1, 3, 3},
{2, 1, 1},
{2, 2, 1},
{2, 3, 1},
{2, 4, 1}
};
auto results = countUniqueSubjects(teacherRecords);
for (const auto& result : results) {
std::cout << "Teacher " << result.first << " teaches " << result.second << " unique subjects\n";
}
return 0;
}
This C++ solution employs std::unordered_map and std::unordered_set to reconstruct SQL-like operations by grouping and deduplication of subjects for each teacher. It fills the map with teacher IDs as keys and sets of unique subject IDs. Finally, the size of each set is used to determine the number of unique subjects taught by every teacher.