Number of Occurrences in Array - Time Complexity: O(n) - Language: Java


Given an array of integers nums, count and return the number of occurrences of a given value.

Example:

Input: nums = [1, 4, 2, 2, 5, 2], value = 2
Output: 3
Explanation: the value 2 appears 3 times in the array

Note:

Do not use builtin functions, it would defy the purpose of the challenge. Write the whole code yourself.

Understanding the Problem

The core challenge of this problem is to count the number of times a specific value appears in an array of integers. This is a common problem in data processing and analysis, where you need to determine the frequency of certain elements.

Potential pitfalls include not correctly iterating through the entire array or miscounting the occurrences due to logic errors.

Approach

To solve this problem, we can use a simple linear scan of the array. This approach ensures that we check each element exactly once, making it efficient with a time complexity of O(n), where n is the number of elements in the array.

Here is a step-by-step approach:

  1. Initialize a counter to zero.
  2. Iterate through each element in the array.
  3. For each element, check if it matches the given value.
  4. If it matches, increment the counter.
  5. After the loop, the counter will hold the number of occurrences of the given value.

Algorithm

Let's break down the algorithm in detail:

  1. Start with a counter set to zero.
  2. Loop through each element of the array using a for loop.
  3. Inside the loop, use an if statement to compare the current element with the given value.
  4. If they are equal, increment the counter by one.
  5. After the loop ends, return the counter as the result.

Code Implementation

public class NumberOfOccurrences {
    public static int countOccurrences(int[] nums, int value) {
        // Initialize the counter to zero
        int count = 0;
        
        // Iterate through each element in the array
        for (int num : nums) {
            // Check if the current element matches the given value
            if (num == value) {
                // Increment the counter if a match is found
                count++;
            }
        }
        
        // Return the final count
        return count;
    }

    public static void main(String[] args) {
        // Example usage
        int[] nums = {1, 4, 2, 2, 5, 2};
        int value = 2;
        int result = countOccurrences(nums, value);
        System.out.println("The value " + value + " appears " + result + " times in the array.");
    }
}

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of elements in the array. This is because we are iterating through the array once. The space complexity is O(1) since we are using only a single counter variable and no additional data structures.

Edge Cases

Consider the following edge cases:

Examples:

Input: nums = [], value = 2
Output: 0

Input: nums = [1, 3, 4, 5], value = 2
Output: 0

Input: nums = [2, 2, 2, 2], value = 2
Output: 4

Testing

To test the solution comprehensively, consider the following test cases:

Example test cases:

assert countOccurrences(new int[]{1, 4, 2, 2, 5, 2}, 2) == 3;
assert countOccurrences(new int[]{}, 2) == 0;
assert countOccurrences(new int[]{1, 3, 4, 5}, 2) == 0;
assert countOccurrences(new int[]{2, 2, 2, 2}, 2) == 4;

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to count the number of occurrences of a given value in an array. We explored a simple and efficient approach with a time complexity of O(n). Understanding and solving such problems is crucial for data processing and analysis tasks. Practice and explore further to enhance your problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: