इस खंड में हम देखेंगे कि कितने तत्वों के निरपेक्ष मान विशिष्ट हैं? मान लीजिए एक सरणी में कुछ तत्व हैं जैसे {5, 5, 6, -5, 8, 2, -2, 1}, तो 8 तत्व हैं। लेकिन 5 तत्व {5, 6, 8, 2, 1} हैं जो विशिष्ट हैं। -5 और 5 को अलग नहीं माना जाता, वे समान हैं क्योंकि उनका निरपेक्ष मान समान है।
इस समस्या को हल करने के लिए, हम सेट डेटा-संरचना का उपयोग करेंगे। सेट में डुप्लिकेट तत्वों की अनुमति नहीं है। और जब हम सेट में आइटम डाल रहे हैं, तो हम केवल निरपेक्ष मान डालेंगे।
एल्गोरिदम
absoluteDistinctCount(arr)
begin define a set s; for each element e in arr, do insert |e| into s done return the number of elements of s end
उदाहरण
#include#include #include using namespace std; int absoluteDistinctCount(int arr[], int n){ set s; for(int i = 0; i आउटपुट
Absolute Distinct Count: 5