Counting Element Occurrences in an Array
This challenge focuses on efficiently determining the number of times each unique element appears within a given array. Counting occurrences is a fundamental operation in data analysis and algorithm design, useful for tasks like frequency analysis, identifying common patterns, and building histograms. Your task is to write a JavaScript function that accurately counts the occurrences of each element in an array and returns the results in a structured format.
Problem Description
You are required to implement a JavaScript function named countOccurrences that takes an array of any data type as input and returns an object. This object should have keys representing the unique elements found in the input array, and the corresponding values should represent the number of times each element appears in the array.
Key Requirements:
- The function must handle arrays containing elements of any data type (numbers, strings, booleans, objects, etc.).
- The function must be case-sensitive when dealing with strings.
- The function should return an empty object if the input array is empty.
- The function should not modify the original input array.
Expected Behavior:
The function should iterate through the input array and keep track of the occurrences of each element. It should then construct an object where each unique element is a key, and its value is the number of times it appeared in the array.
Edge Cases to Consider:
- Empty input array.
- Array with duplicate elements.
- Array with mixed data types.
- Array containing
nullorundefinedvalues. - Array containing objects (consider object equality - two objects with the same properties and values should be considered the same).
Examples
Example 1:
Input: [1, 2, 2, 3, 3, 3]
Output: { '1': 1, '2': 2, '3': 3 }
Explanation: The number 1 appears once, the number 2 appears twice, and the number 3 appears three times.
Example 2:
Input: ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
Output: { 'apple': 3, 'banana': 2, 'orange': 1 }
Explanation: The string 'apple' appears three times, 'banana' appears twice, and 'orange' appears once.
Example 3:
Input: [1, '1', true, 1, null, undefined, true]
Output: { '1': 1, 'true': 2, 'null': 1, 'undefined': 1, '1': 1 }
Explanation: Demonstrates handling mixed data types and differentiating between string '1' and number 1. Note that the keys are strings.
Constraints
- The input array can contain up to 10,000 elements.
- The elements of the array can be of any valid JavaScript data type.
- The function should have a time complexity of O(n), where n is the number of elements in the array. (While a nested loop solution might work, it will be less efficient and may not meet the performance expectations for larger arrays.)
- The function should not use any external libraries or frameworks.
Notes
Consider using a JavaScript object (or a Map) to store the counts of each element. Iterating through the array once and updating the counts in the object is a common and efficient approach. Remember to handle the case where an element is encountered for the first time – you'll need to initialize its count to 0 before incrementing it. Pay close attention to how JavaScript handles equality comparisons for different data types.