Grokking Oracle Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Degree of an Array (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

You are given an array nums containing positive integers. The degree of nums is defined as the maximum frequency of any one of its elements.

Return the minimum length of a contiguous subarray of nums, that has the same degree as nums.

Example

  • Example 1:

    • Input: nums = [1, 4, 2, 3, 2]
    • Expected Output: 3
    • Justification: The array's degree is 2 as it appears twice. The shortest subarray with a degree of 2 is [2, 3, 2], which has a length of 3.
  • Example 2:

    • Input: nums = [4, 9, 4, 4, 5, 5, 4]
    • Expected Output: 7
    • Justification: Here, the number 4 appears the most (four times). The shortest contiguous segment that includes four 4s is the array itself. So, the answer is 4.
  • Example 3:

    • Input: nums = [6, 1, 1, 2, 1, 2, 2]
    • Expected Output: 4
    • Justification: The number 1 appears the most (three times). The shortest subarray capturing all instances of 1 is [1, 1, 2, 1], which has a length of 4.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible