Knowledge Guide
HomeDSARecursion

Solution Number Frequency

Problem Statement

Write a Recursive Solution to Count occurrences of an Element in an Array.

Given an array of integers and a key element, write a recursive solution to count the occurrences of the key element in the array.

Examples:

Sr#ArrayInput KeyOutputDescription
1[2, 4, 6, 8, 4]42The key element 4 occurs twice in the array.
2[1, 3, 5, 7, 9]20The key element 2 does not exist in the array.
3[1, 2, 2, 2, 3]23The key element 2 occurs three times in the array.

Solution

  1. Base Case:
    • If the array is empty, return 0 because there are no occurrences of the key element.
  2. Recursive Case:
    • Compare the first element of the array with the key element.
      • If they are equal, increment the count by 1.
    • Make a recursive call with the remaining subarray.
    • Add the count from the recursive call to the current count and return the total count.
Image
Image

Code

Here is the code for this algorithm:

java
public class Solution {

  public static int countOccurrences(int[] arr, int key) {
    return countOccurrencesHelper(arr, key, 0);
  }

  private static int countOccurrencesHelper(int[] arr, int key, int index) {
    // Base case: If the array is empty, return 0
    if (index >= arr.length) {
      return 0;
    }

    // Check if the current element is equal to the key element
    int count = arr[index] == key ? 1 : 0;

    // Recursive call with the remaining subarray
    int totalCount = count + countOccurrencesHelper(arr, key, index + 1);

    return totalCount;
  }

  public static void main(String[] args) {
    int[] arr1 = { 2, 4, 6, 8, 4 };
    int key1 = 4;
    System.out.println("Occurrences in arr1: " + countOccurrences(arr1, key1));

    int[] arr2 = { 1, 3, 5, 7, 9 };
    int key2 = 2;
    System.out.println("Occurrences in arr2: " + countOccurrences(arr2, key2));

    int[] arr3 = { 1, 2, 2, 2, 3 };
    int key3 = 2;
    System.out.println("Occurrences in arr3: " + countOccurrences(arr3, key3));
  }
}

Time and Space Complexity

The time complexity of the algorithm is because, in the worst case, it needs to examine all elements of the array once. The space complexity of the algorithm is because the recursive calls consume additional space on the call stack, and in the worst case, the depth of the call stack is proportional to the size of the array.

🤖 Don't fully get this? Learn it with Claude

Stuck on Solution Number Frequency? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Solution Number Frequency** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Solution Number Frequency** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Solution Number Frequency**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Solution Number Frequency**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes