Count unread messages by channel
Build per-channel unread badge counts from a flat message collection.EasyMessaging Analytics
20 min0% acceptance
Description
Implement countUnreadByChannel so it returns an object keyed by every channel encountered in the message list. Each value is the number of messages in that channel whose read field is false, including a count of zero for channels whose messages are all read.
Requirements
The solution exports a callable function named countUnreadByChannel.
Every channel encountered in the input appears as a key in the result.
Only messages whose read value is false increase a channel's count.
Channels containing only read messages are included with a count of zero.
An empty input array returns an empty object.
The function does not mutate the input array or any message object.
Two channels
Input
[{ channelId: 'general', read: false }, { channelId: 'general', read: true }, { channelId: 'product', read: false }]Expected output
{ general: 1, product: 1 }Constraints
Every message has a non-empty channelId string and a boolean read fieldChannel ids contain letters, digits, hyphens, or underscoresThe input may contain up to 50000 messagesDo not mutate caller-owned data
Hints
Hint 1
Initialize a channel when it is first encountered, even if that message is already read.
Hint 2
Increment only for an explicit unread state instead of deriving the output keys from unread messages.
Hint 3
A single pass can preserve every encountered channel while accumulating its count.
arraysobjectscounting