Problem · Hash Table

Maximum Elements With a Common Digit

Learn this problem
EasyGoogleOA
See Google hiring insights

Problem statement

You are given an array numbers of length N. A set of array elements can be chosen together if the chosen elements share at least one common digit.

For example, 52, 25, and 55 can be chosen together because they share digit 5, but 11, 52, and 34 cannot.

What is the maximum number of array elements that can be chosen together?

Write a function solution(numbers) that, given an array numbers of length N, returns the maximum number of elements that can be chosen.

Source note: The screenshot is missing part of the question, but the examples are clear enough for practice. If you know the full original version, feel free to tell us; we'll update it when we find a clearer source. 🦤

Function

solution(numbers: int[]) → int

Examples

Example 1

numbers = [52, 25, 11, 52, 34, 55]return = 4

Elements 52, 25, 52, and 55 can be chosen.

Example 2

numbers = [71, 23, 57, 15]return = 2

It is possible to choose at most two elements.

Example 3

numbers = [11, 33, 55]return = 1

No two numbers share any digit.

Example 4

numbers = [90, 90, 90]return = 3

All numbers can be chosen.

Constraints

  • N is an integer within the range [1..100].
  • Each element of array numbers is an integer within the range [10..99].

More Google problems

drafts saved locally
public int solution(int[] numbers) {
  // write your code here
}
numbers[52, 25, 11, 52, 34, 55]
expected4
checking account