Skip to content
CodeKruPro

Count how often each value occurs

Coding problemEasyAsked in: Capgemini, Infosys

Given a list of integers, count how many times each distinct value occurs.

Return one entry per distinct value, ordered from the smallest value to the largest. Write each entry as the value, an equals sign and the count, such as "-3=2". An empty list gives an empty list.

Examples

Example 1
Input: nums = [3,1,3,2,3]
Output: ["1=1","2=1","3=3"]
Example 2
Input: nums = [-5,10,-5]
Output: ["-5=2","10=1"]
Example 3
Input: nums = []
Output: []

Constraints

  • The list holds between 0 and 100,000 integers
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in