- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Absolute distinct count in a sorted array?

In this section we will see how to count how many of elements whose absolute values are distinct? Suppose in an array there are few elements like {5, 5, 6, -5, 8, 2, -2, 1}, so there are 8 elements. But there are 5 elements {5, 6, 8, 2, 1} which are distinct. The -5 and 5 are not considered as different, they are same as their absolute value is same.

To solve this problem, we will use the Set data-structure. In set duplicate elements are not allowed. And when we are inserting item into the set, we will push only the absolute value.

## Algorithm

#### absoluteDistinctCount(arr)

begin define set s; for each element e in arr, do insert |e| into s done return the number of elements of s end

## Example

#include<iostream> #include<set> #include<cmath> using namespace std; int absoluteDistinctCount(int arr[], int n){ set<int> s; for(int i = 0; i<n; i++){ s.insert(abs(arr[i])); //insert the absolute value } return s.size(); } main() { int arr[] = {5, 5, 6, -5, 8, 2, -2, 1}; int n = (sizeof(arr))/(sizeof(arr[0])); cout << "Absolute Distinct Count: " << absoluteDistinctCount(arr, n); }

## Output

Absolute Distinct Count: 5

- Related Questions & Answers
- Absolute distinct count in a sorted array in C++?
- Print sorted distinct elements of array in C language
- Program to find sum of absolute differences in a sorted array in Python
- Count distinct elements in an array in Python
- Count distinct elements in an array in C++
- Count smaller elements in sorted array in C++
- Count 1’s in a sorted binary array in C++
- Select two fields and return a sorted array with their distinct values in MongoDB?
- Count number of occurrences (or frequency) in a sorted array in C++
- Find the Rotation Count in Rotated Sorted array in C++
- Count distinct value in MongoDB?
- C++ code to count operations to make array sorted
- Get Distinct Values with Sorted Data in MongoDB?
- Write a program in Python to print numeric index array with sorted distinct values in a given series
- Count of only repeated element in a sorted array of consecutive elements in C++

Advertisements