Absolute distinct count in a sorted array | C # code to find absolute distinct count of an array in O ( n ) time . ; The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int distinctCount ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( Math . Abs ( arr [ i ] ) ) ; return s . Count ; } public static void Main ( ) { int [ ] arr = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . Write ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }