JavaScript Sets & Maps (ES6): Unique Users Challenge
• Updated 6/18/2026 • JavaScriptES6SetsMapsData StructuresCoding ChallengePractice
What you will build
In this coding challenge, you will practice two powerful ES6 data structures: Set and Map.
You will build a function called analyzeUsers(users) that:
- Removes duplicate usernames using a
Set. - Counts how many times each username appears using a
Map. - Returns both the unique usernames and their occurrence counts.
This challenge mirrors real-world tasks such as processing user registrations, analytics data, and activity logs.
Concept overview
ES6 introduced two useful data structures:
- Set – stores unique values only. Duplicate values are automatically ignored.
- Map – stores key-value pairs and is useful for counting, grouping, and lookups.
Unlike plain objects, Maps can use any data type as a key and preserve insertion order.
Example
const users = ["alice", "bob", "alice"];
const uniqueUsers = [...new Set(users)];
// Result:
// ["alice", "bob"]
Coding challenge
const counts = new Map();
counts.set("alice", 2);
counts.set("bob", 1);
Requirements
Input:
["alice", "bob", "alice", "charlie", "bob", "alice"]
Expected output:
{
uniqueUsers: ["alice", "bob", "charlie"],
counts: {
alice: 3,
bob: 2,
charlie: 1
}
}
Hints
Write a function analyzeUsers(users) that receives an array of usernames and returns:
- An array of unique usernames.
- A count of how many times each username appears.
Your solution should use both a Set and a Map.
Solution walkthrough
Starter code
users– an array of strings representing usernames.
Solution
- Return an object with two properties:
uniqueUsers– an array containing unique usernames.counts– an object containing username frequencies.
Stretch goals
- The input array may be empty.
- Usernames are case-sensitive.
- The array may contain duplicate usernames.
- The solution should work for arrays containing up to several thousand entries.
Hints
- Create a
Setfrom the input array to get unique usernames. - Create a
Mapto track counts. - Loop through the array and update the count for each username.
- Convert the Map into a plain object before returning the result.
Solution Walkthrough
- Create a Set using the input array to remove duplicates.
- Create an empty Map called
counts. - Loop through every username.
- If the username already exists in the Map, increment its count.
- Otherwise, add it with a starting count of 1.
- Convert the Set into an array.
- Convert the Map into a plain object and return both results.
Starter Code
/**
* Analyze a list of usernames.
*
* @param {string[]} users
* @returns {{ uniqueUsers: string[], counts: Object }}
*/
function analyzeUsers(users) {
// TODO: Create a Set for unique users
// TODO: Create a Map for counts
// TODO: Count occurrences
// TODO: Return the final result
}
const users = ["alice", "bob", "alice", "charlie"];
console.log(analyzeUsers(users));
Solution
function analyzeUsers(users) {
const uniqueUsers = [...new Set(users)];
const counts = new Map();
for (const user of users) {
counts.set(user, (counts.get(user) || 0) + 1);
}
return {
uniqueUsers,
counts: Object.fromEntries(counts)
};
}
Example Usage
const users = [
"alice",
"bob",
"alice",
"charlie",
"bob",
"alice"
];
console.log(analyzeUsers(users));
Stretch Goals
- Sort usernames by frequency from highest to lowest.
- Ignore letter casing by treating
"Alice"and"alice"as the same user. - Return the most common username.
- Group users by their first letter using a Map.
- Compare the performance of a Map-based solution versus a plain object solution.

