Given an array of integers, count how many distinct values exist in the array.
Example:
Input: [1, 5, -3, 1, -4, 2, -4, 7, 7] Output: 6 Explanation: the distinct values in the array are [1, 5, -3, -4, 2, 7]
For this lesson, your algorithm should run in O(n log n) time and use O(1) extra space.
(There are faster solutions which we will discuss in future lessons)
i
. We should increment the solution if value nums[i]
has not been seen before. How can we check this?
nums[i]
has never been seen before if nums[i] != nums[i - 1]
.
The task is to count the number of distinct values in a given array of integers.
Input: An array of integers.
Output: An integer representing the number of distinct values in the array.
Constraints:
Example:
Input: [1, 5, -3, 1, -4, 2, -4, 7, 7] Output: 6 Explanation: The distinct values in the array are [1, 5, -3, -4, 2, 7]
The core challenge is to identify and count unique elements in the array efficiently. This problem is significant in various applications such as data analysis, where understanding the diversity of data points is crucial. A common pitfall is to use additional data structures like sets, which can increase space complexity.
To solve this problem, we can use the following approach:
Let's discuss a naive solution and its limitations:
Naive Solution: Use a set to store unique elements and return its size. This approach has O(n) time complexity but uses O(n) space, which violates the space constraint.
Optimized Solution: By sorting the array first, we can achieve O(n log n) time complexity and use O(1) extra space by counting distinct elements during a single traversal of the sorted array.
Here is a step-by-step breakdown of the optimized algorithm:
#include <iostream>
#include <vector>
#include <algorithm>
int countDistinctValues(std::vector<int>& nums) {
// Step 1: Sort the array
std::sort(nums.begin(), nums.end());
// Step 2: Initialize the counter for distinct values
int distinctCount = 0;
// Step 3: Traverse the sorted array
for (size_t i = 0; i < nums.size(); ++i) {
// If it's the first element or different from the previous one, it's distinct
if (i == 0 || nums[i] != nums[i - 1]) {
++distinctCount;
}
}
return distinctCount;
}
int main() {
std::vector<int> nums = {1, 5, -3, 1, -4, 2, -4, 7, 7};
std::cout << "Number of distinct values: " << countDistinctValues(nums) << std::endl;
return 0;
}
Time Complexity: The sorting step takes O(n log n) time, and the traversal step takes O(n) time. Thus, the overall time complexity is O(n log n).
Space Complexity: The algorithm uses O(1) extra space as it sorts the array in place and uses a constant amount of additional memory for the counter.
Consider the following edge cases:
Examples:
Input: [] Output: 0 Input: [2, 2, 2, 2] Output: 1 Input: [1, 2, 3, 4, 5] Output: 5
To test the solution comprehensively, consider a variety of test cases:
Use a testing framework like Google Test for C++ to automate and manage test cases efficiently.
When approaching such problems:
In this blog post, we discussed how to count the number of distinct values in an array using an efficient algorithm with O(n log n) time complexity and O(1) extra space. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.
Keep practicing and exploring further to enhance your understanding and proficiency in algorithms and data structures.
For further reading and practice, consider the following resources: