Problem · Array

Most Frequent Reduced Digit

Learn this problem
EasyAirbnbNEW GRADOA

Problem statement

You are working with data collected from various sensors. Given an array of non-negative integers readings, repeatedly replace each value with the sum of its decimal digits until every value is a single digit.

Return the most frequent digit in the final transformed array. If several digits have the same highest frequency, return the highest such digit.

A solution with time complexity no worse than O(readings.length^2) fits within the execution time limit.

Function

mostFrequentReducedDigit(readings: int[]) → int

Examples

Example 1

readings = [123,456,789,101]return = 6
  • 123 becomes 1 + 2 + 3 = 6.
  • 456 becomes 4 + 5 + 6 = 15, then 1 + 5 = 6.
  • 789 becomes 7 + 8 + 9 = 24, then 2 + 4 = 6.
  • 101 becomes 1 + 0 + 1 = 2.

The final array is [6,6,6,2], so the most frequent digit is 6.

Example 2

readings = [6]return = 6

The reading 6 is already a single digit, so the final array remains [6] and the result is 6.

Example 3

readings = [3,12,23,32,0]return = 5

The readings reduce to [3,3,5,5,0]. Digits 3 and 5 each occur twice, so the tie is resolved in favor of the higher digit, 5.

Constraints

  • Every value in readings is a non-negative integer.

More Airbnb problems

drafts saved locally
public int mostFrequentReducedDigit(int[] readings) {
    // write your code here
}
readings[123,456,789,101]
expected6
checking account