count the number of distinct absolute values among the elements of the array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Counting distinct absolute values means treating -5 and 5 as the same value, then asking how many unique magnitudes remain. It is a small problem, but it shows up often in interview questions because the best solution depends on whether the array is already sorted.
For unsorted input, a set-based approach is simple and correct. For sorted input, a two-pointer solution can do the same job in linear time with constant extra space.
Simple Set-Based Solution
If the input can be in any order, the most direct approach is to take the absolute value of each element and insert it into a set.
This works because a set keeps only unique values. Its time complexity is O(n) on average, and the extra space is also O(n).
That is usually the right answer unless the problem explicitly asks for lower space usage or tells you the input is sorted already.
Better Space Usage for a Sorted Array
When the array is sorted, the largest absolute value must be at one of the ends. That makes a two-pointer scan possible.
The logic is:
- compare the absolute values at both ends
- count the larger magnitude once
- skip all duplicates of that magnitude on both sides
Because each index moves inward only once, the algorithm stays O(n) and uses O(1) extra space.
Walk Through an Example
Take the sorted array [-4, -4, -2, 0, 2, 2, 5].
The distinct absolute values are 4, 2, 0, and 5, so the answer is 4.
With the two-pointer method:
- start at
-4and5, count5 - move past all
5values on the right - compare
-4and2, count4 - skip both
-4values - compare
-2and2, count2once - skip every
2and-2 - finally count
0
The benefit is that you never build another array and never insert into a hash set.
Choose the Algorithm Based on the Input Contract
A lot of confusion comes from mixing the two scenarios. If the input is not sorted, the two-pointer method is wrong unless you sort first. Sorting gives you a valid solution, but it changes the time complexity to O(n log n).
So the practical rule is:
- unsorted input and simplicity matters: use a set
- sorted input and space matters: use two pointers
That is a better answer than forcing one technique for every case.
Common Pitfalls
The biggest mistake is forgetting that absolute values can collide across signs. If you just count distinct original numbers, -2 and 2 are incorrectly treated as different.
Another common issue is using the two-pointer solution on unsorted input. That method relies completely on the sorted order.
In fixed-width integer languages, be careful with the smallest negative integer because abs can overflow. Python handles big integers safely, but languages such as Java and C# need extra care around their minimum integer values.
Finally, do not forget to skip duplicates on both sides in the sorted solution. If you only move one pointer, you can double-count the same magnitude.
Summary
- Distinct absolute values treat positive and negative versions of the same magnitude as one value.
- A set-based solution is the simplest choice for unsorted arrays.
- A sorted array allows a linear two-pointer solution with constant extra space.
- The algorithm choice depends on the guarantees the input gives you.
- Handle duplicates carefully or the count will be wrong.
Related reading
- Count the number of Ks between 0 and N
- Count the number of occurrences of a character in a string
- Count the number of set bits in a 32-bit integer
- Count the number of set bits in a 32-bit integer
- Counting alternating numbers in an array
- Counting array elements in Python
- Count the subsequences of length 4 divisible by 9
- count the total number of 1's in integers from 1 to N

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.