Given a sorted array of N integers, find the number of distinct absolute values among the elements of the array. 
Example 1:
Input:
N = 6
arr[] = {-3, -2, 0, 3, 4, 5}
Output: 5
Explanation: There are 5 distinct absolute 
values among the elements of this array, 
i.e. 0, 2, 3, 4 and 5.
Example 2:
Input:
N = 9
arr[] = {-1, -1, -1, -1, 0, 1, 1, 1, 1}
Output: 2
Explanation: There are 2 distinct absolute values 
among the elements of this array, i.e. 0 and 1.
Your Task:
You don't need to read input or print anything. Complete the function distinctCount() which takes the array of integers arr[] and its size n as input parameters and returns the number of distinct absolute values.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ |Arr[i]| ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    def distinctCount(self, arr, n):

        # code here

Solutions:
class Solution:

	def distinctCount(self, arr, n):
		a = [abs(i) for i in arr]
		b = set(a)
		return len(b)


class Solution:

	def distinctCount(self, arr, n):
		s = set()
		for i in arr:
			s.add(abs(i))
		return len(s)


class Solution:

	def distinctCount(self, arr, n):
		c = []
		for i in arr:
			c.append(abs(i))
		return len(set(c))


class Solution:

	def distinctCount(self, arr, n):
		res = []
		for i in arr:
			res.append(abs(i))
		a = set(res)
		return len(a)


class Solution:

	def distinctCount(self, arr, n):
		for i in range(n):
			arr[i] = abs(arr[i])
		arr = set(arr)
		return len(arr)


class Solution:

	def distinctCount(self, arr, n):
		return len(set(map(abs, arr)))


class Solution:

	def distinctCount(self, arr, n):
		l = list(set(arr))
		res = []
		for i in l:
			res.append(abs(i))
		return len(set(res))


class Solution:

	def distinctCount(self, arr, n):
		dict = {}
		for element in arr:
			value = abs(element)
			if value not in dict:
				dict[value] = 1
		return len(dict)


class Solution:

	def distinctCount(self, arr, n):
		ans = set()
		for e in arr:
			ans.add(abs(e))
		return len(ans)


class Solution:

	def distinctCount(self, arr, n):
		dic = {}
		for i in range(n):
			if abs(arr[i]) not in dic:
				dic[abs(arr[i])] = 1
		return len(dic)


class Solution:

	def distinctCount(self, arr, n):
		start = 0
		end = n - 1
		count = 0
		while start < end:
			if arr[start] == arr[start + 1]:
				start += 1
				continue
			if arr[end] == arr[end - 1]:
				end -= 1
				continue
			if abs(arr[start]) == abs(arr[end]):
				count += 1
				start += 1
				end -= 1
			elif abs(arr[start]) > abs(arr[end]):
				count += 1
				start += 1
			else:
				count += 1
				end -= 1
		if start == end:
			count += 1
		return count


class Solution:

	def distinctCount(self, arr, n):
		t = set(arr)
		h = [abs(i) for i in list(t)]
		return len(set(h))


class Solution:

	def distinctCount(self, arr, n):
		for i in range(n):
			if arr[i] < 0:
				arr[i] = -arr[i]
		a = set(arr)
		return len(a)


class Solution:

	def distinctCount(self, arr, n):
		ab = []
		for i in arr:
			ab.append(abs(i))
		return len(list(set(ab)))


class Solution:

	def distinctCount(self, arr, n):
		x = {}
		for i in arr:
			i = abs(i)
			if i not in x:
				x[i] = 1
		return len(x)


class Solution:

	def distinctCount(self, arr, n):
		a = set()
		for i in arr:
			if abs(i) not in a:
				a.add(abs(i))
		return len(a)


class Solution:

	def distinctCount(self, arr, n):
		s = set()
		for i in range(n):
			s.add(abs(arr[i]))
		arr2 = list(s)
		return len(arr2)


class Solution:

	def distinctCount(self, arr, n):
		i = 0
		j = n - 1
		c = 0
		pre = -999999
		next = +99999
		while i <= j:
			if abs(arr[i]) == abs(arr[j]):
				if arr[i] != pre and arr[j] != next:
					c += 1
				pre = arr[i]
				next = arr[j]
				i += 1
				j -= 1
			elif abs(arr[i]) < abs(arr[j]):
				if arr[j] != next:
					c += 1
				next = arr[j]
				j -= 1
			else:
				if arr[i] != pre:
					c += 1
				pre = arr[i]
				i += 1
		return c


class Solution:

	def distinctCount(self, arr, n):
		for i in range(n):
			if arr[i] < 0:
				arr[i] = abs(arr[i])
		arr.sort()
		count = 1
		for i in range(n - 1):
			if arr[i] != arr[i + 1]:
				count += 1
		return count


class Solution:

	def distinctCount(self, arr, n):
		c = 0
		a = []
		for i in range(n):
			a.append(abs(arr[i]))
		for i in set(a):
			c += 1
		return c


class Solution:

	def distinctCount(self, arr, n):
		left = 0
		right = n - 1
		count = 0
		prev = 1 + max(abs(arr[left]), abs(arr[right]))
		while left <= right:
			if abs(arr[left]) <= abs(arr[right]):
				cur = abs(arr[right])
				right -= 1
			else:
				cur = abs(arr[left])
				left += 1
			if cur != prev:
				count += 1
			prev = cur
		return count


class Solution:

	def distinctCount(self, arr, n):
		count = 0
		num_set = set()
		for x in arr:
			if abs(x) not in num_set:
				count += 1
				num_set.add(abs(x))
		return count


from collections import Counter

class Solution:

	def distinctCount(self, arr, n):
		abs_val = map(abs, arr)
		cnt = Counter(abs_val)
		return len(cnt.keys())


class Solution:

	def distinctCount(self, arr, n):
		r = [abs(x) for x in arr]
		return len(set(r))
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ob = Solution()
		ans = ob.distinctCount(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def distinctCount(self, arr, n):
		for i in range(n):
			if arr[i] < 0:
				arr[i] = -arr[i]
		return len(set(arr))
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ob = Solution()
		ans = ob.distinctCount(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def distinctCount(self, arr, n):
		for i in range(n):
			if arr[i] < 0:
				arr[i] = arr[i] * -1
		arr.sort()
		arr = sorted(list(set(arr)))
		return len(arr)


class Solution:

	def distinctCount(self, arr, n):
		l = 0
		r = n - 1
		c = n
		while l < r:
			while l < r and arr[r] == arr[r - 1]:
				r -= 1
				c -= 1
			while l < r and arr[l] == arr[l + 1]:
				l += 1
				c -= 1
			if l == r:
				break
			if abs(arr[l]) < abs(arr[r]):
				r -= 1
			elif abs(arr[l]) > abs(arr[r]):
				l += 1
			else:
				l += 1
				r -= 1
				c -= 1
		return c


class Solution:

	def distinctCount(self, arr, n):
		count = 0
		unique_arr = set()
		for val in arr:
			unique_arr.add(abs(val))
		return len(unique_arr)


class Solution:

	def distinctCount(self, arr, n):
		li = [abs(ele) for ele in arr]
		li = set(li)
		return len(li)


class Solution:

	def distinctCount(self, arr, n):
		result = set()
		for i in range(n):
			if arr[i] not in result:
				result.add(abs(arr[i]))
		count = 0
		for j in result:
			count += 1
		return count


class Solution:

	def distinctCount(self, arr, n):
		counter = {}
		for i in arr:
			counter.setdefault(abs(i), 0)
		c = 0
		for key in counter:
			if key >= 0:
				c += 1
		return c


class Solution:

	def distinctCount(self, arr, n):
		gg = {}
		for i in arr:
			if i < 0:
				i = -i
			if i not in gg:
				gg[i] = 1
		return len(gg)


class Solution:

	def distinctCount(self, arr, n):
		l = 0
		r = n - 1
		count = 0
		prev = float('-inf')
		nxt = float('inf')
		while l <= r:
			if abs(arr[l]) == abs(arr[r]):
				if prev != arr[l] and nxt != arr[r]:
					count += 1
				prev = arr[l]
				nxt = arr[r]
				l += 1
				r -= 1
			elif abs(arr[l]) < abs(arr[r]):
				if arr[r] != nxt:
					count += 1
				nxt = arr[r]
				r -= 1
			else:
				if arr[l] != prev:
					count += 1
				prev = arr[l]
				l += 1
		return count


class Solution:

	def distinctCount(self, arr, n):
		(i, j) = (0, n - 1)
		ans = 0
		while i <= j:
			ans += 1
			if abs(arr[i]) != abs(arr[j]):
				if abs(arr[i]) > abs(arr[j]):
					value = abs(arr[i])
					while i < n and abs(arr[i]) == value:
						i += 1
				else:
					value = abs(arr[j])
					while j >= 0 and abs(arr[j]) == value:
						j -= 1
			else:
				value = abs(arr[i])
				while i < n and abs(arr[i]) == value:
					i += 1
				while j >= 0 and abs(arr[j]) == value:
					j -= 1
		return ans


from collections import Counter

class Solution:

	def distinctCount(self, arr, n):
		ans = set()
		c = 0
		for i in range(n):
			if arr[i] not in ans:
				ans.add(abs(arr[i]))
		for j in ans:
			c += 1
		return c


class Solution:

	def distinctCount(self, arr, n):
		l = []
		for i in arr:
			l.append(abs(i))
		l_set = set(l)
		l_count = len(l_set)
		return l_count


class Solution:

	def distinctCount(self, arr, n):
		p = []
		k = []
		for i in range(n):
			p.append(abs(arr[i]))
		k = list(set(list(p)))
		l = len(k)
		return l