Given an array A[] of N positive integers which can contain integers from 1 to P where elements can be repeated or can be absent from the array. Your task is to count the frequency of all elements from 1 to N.
Note: The elements greater than N in the array can be ignored for counting and do modify the array in-place.
Example 1:
Input:
N = 5
arr[] = {2, 3, 2, 3, 5}
P = 5
Output:
0 2 2 0 1
Explanation: 
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 2 times.
3 occurring 2 times.
4 occurring 0 times.
5 occurring 1 time.
Example 2:
Input:
N = 4
arr[] = {3,3,3,3}
P = 3
Output:
0 0 4 0
Explanation: 
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 0 times.
3 occurring 4 times.
4 occurring 0 times.
Your Task:
You don't need to read input or print anything. Complete the function frequencycount() that takes the array and n as input parameters and modify the array itself in place to denote the frequency count of each element from 1 to N. i,e element at index 0 should represent the frequency of 1, and so on.
Can you solve this problem without using extra space (O(1) Space)?
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ P ≤ 4*10^{4}^{ }
1 <= A[i] <= P

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: class Solution:

    #Function to count the frequency of all elements from 1 to N in the array.

    def frequencyCount(self, arr, N, P):

        # code here





Solutions:
class Solution:

	def frequencyCount(self, arr, N, P):
		arr.sort()
		c = 0
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in range(1, N + 1):
			if i in d.keys():
				arr[i - 1] = d[i]
			else:
				arr[i - 1] = 0
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		l = {}
		for num in arr:
			l[num] = l.get(num, 0) + 1
		for index in range(N):
			if index + 1 in l:
				arr[index] = l[index + 1]
			else:
				arr[index] = 0
		return arr


from collections import Counter

class Solution:

	def frequencyCount(self, arr, N, P):
		c = Counter(arr)
		for i in range(1, N + 1):
			arr[i - 1] = c[i]
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		freq_map = {}
		for elem in arr:
			if elem not in freq_map:
				freq_map[elem] = 1
			else:
				freq_map[elem] += 1
		for i in range(N):
			if i + 1 in freq_map:
				arr[i] = freq_map[i + 1]
			else:
				arr[i] = 0


class Solution:

	def frequencyCount(self, arr, N, P):
		res = [0] * N
		for i in range(N):
			if arr[i] <= N:
				res[arr[i] - 1] = res[arr[i] - 1] + 1
		arr.clear()
		for i in res:
			arr.append(i)


class Solution:

	def frequencyCount(self, arr, N, P):
		a = {}
		for i in range(N):
			if arr[i] <= N:
				a[arr[i]] = a.get(arr[i], 0) + 1
		for i in range(1, N + 1):
			if i not in a:
				arr[i - 1] = 0
			else:
				arr[i - 1] = a[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		p = [0] * N
		for i in range(0, N):
			if arr[i] <= N:
				p[arr[i] - 1] += 1
		for i in range(0, N):
			arr[i] = p[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		freq = [0] * (N + 1)
		for num in arr:
			if num <= N:
				freq[num] += 1
		arr[:] = freq[1:]


class Solution:

	def frequencyCount(self, arr, n, p):
		dict1 = {}
		for i in range(1, n + 1):
			dict1[i] = 0
		for i in range(1, n + 1):
			if arr[i - 1] in dict1:
				dict1[arr[i - 1]] += 1
		for i in range(1, n + 1):
			arr[i - 1] = dict1[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		d = dict()
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		arr.clear()
		for i in range(1, N + 1):
			if i in d:
				arr.append(d[i])
			else:
				arr.append(0)


class Solution:

	def frequencyCount(self, arr, N, P):
		k = [0] * max(P + 1, N + 1)
		for i in arr:
			k[i] += 1
		for i in range(N):
			arr[i] = k[i + 1]
		return


class Solution:

	def frequencyCount(self, arr, N, P):
		d = {i + 1: 0 for i in range(len(arr))}
		for i in arr:
			if i in d:
				d[i] += 1
		for i in range(len(arr)):
			arr[i] = d[i + 1]


class Solution:

	def frequencyCount(self, arr, N, P):
		hashh = {}
		for i in arr:
			hashh[i] = hashh.get(i, 0) + 1
		for j in range(1, N + 1):
			arr[j - 1] = hashh.get(j, 0)


class Solution:

	def frequencyCount(self, arr, N, P):
		frequencyMap = {}
		for num in arr:
			if num <= N:
				frequencyMap[num] = frequencyMap.get(num, 0) + 1
		for i in range(N):
			arr[i] = frequencyMap.get(i + 1, 0)
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		dict1 = {}
		for i in arr:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		for i in range(1, N + 1):
			if i not in dict1:
				arr[i - 1] = 0
			else:
				arr[i - 1] = dict1[i]
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		result = []
		numMap = {}
		for i in range(0, N):
			if arr[i] not in numMap:
				numMap[arr[i]] = 1
			else:
				numMap[arr[i]] += 1
		arr.clear()
		for i in range(1, N + 1):
			arr.append(numMap.get(i, 0))


class Solution:

	def frequencyCount(self, array, n, P):
		result = [0 for i in range(max(array) + 1)]
		for i in array:
			result[i] += 1
		array.clear()
		for i in range(n):
			value = result[i + 1] if i + 1 < len(result) else 0
			array.append(value)
		return


class Solution:

	def frequencyCount(self, arr, N, P):
		d = {}
		for i in arr:
			try:
				d[i] += 1
			except:
				d[i] = 1
		for i in range(0, N):
			try:
				arr[i] = d[i + 1]
			except:
				arr[i] = 0


class Solution:

	def frequencyCount(self, arr, N, P):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		arr.clear()
		for i in range(1, N + 1):
			b = d.get(i)
			if b != None:
				arr.append(b)
			else:
				arr.append(0)
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		hashmap = {}
		res = []
		for i in arr:
			hashmap[i] = 1 + hashmap.get(i, 0)
		for i in range(1, N + 1):
			arr[i - 1] = hashmap.get(i, 0)


class Solution:

	def frequencyCount(self, arr, N, P):
		hash_dict = {}
		for i in range(N):
			if arr[i] in hash_dict:
				hash_dict[arr[i]] += 1
			else:
				hash_dict[arr[i]] = 1
		arr.clear()
		for i in range(1, N + 1):
			temp = hash_dict.get(i)
			if temp != None:
				arr.append(temp)
			else:
				arr.append(0)
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		c = 0
		arr1 = [0] * N
		for i in arr:
			if i <= N:
				arr1[i - 1] = arr1[i - 1] + 1
		for i in range(N):
			arr[i] = arr1[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		hash = {}
		h = []
		for i in arr:
			if i not in hash.keys():
				hash[i] = 1
			else:
				hash[i] = hash.get(i) + 1
		for i in range(N):
			if i + 1 in hash.keys():
				arr[i] = hash[i + 1]
			else:
				arr[i] = 0


class Solution:

	def frequencyCount(self, arr, N, P):
		frequency_map = {}
		for i in arr:
			if i <= P:
				frequency_map[i] = frequency_map.get(i, 0) + 1
		for i in range(N):
			arr[i] = frequency_map.get(i + 1, 0)


class Solution:

	def frequencyCount(self, arr, N, P):
		d = {}
		res_lst = []
		for item in arr:
			if item in d:
				d[item] += 1
			else:
				d[item] = 1
		for i in range(1, N + 1):
			if i in d:
				arr[i - 1] = d[i]
			else:
				arr[i - 1] = 0


class Solution:

	def frequencyCount(self, arr, N, P):
		dict1 = {}
		for each in arr:
			dict1[each] = dict1.get(each, 0) + 1
		for i in range(N):
			arr[i] = dict1.get(i + 1, 0)


class Solution:

	def frequencyCount(self, arr, N, P):
		l = []
		dict1 = {}
		for each in arr:
			if each in dict1:
				dict1[each] += 1
			else:
				dict1[each] = 1
		for i in range(1, N + 1):
			if i not in dict1:
				l.append(0)
			else:
				l.append(dict1[i])
		for i in range(len(l)):
			arr[i] = l[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		m = dict()
		for i in range(N):
			if arr[i] in m:
				m[arr[i]] = m[arr[i]] + 1
			else:
				m[arr[i]] = 1
		for i in range(N):
			arr[i] = m[i + 1] if i + 1 in m else 0


class Solution:

	def frequencyCount(self, arr, N, P):
		i = 0
		while i < N:
			t1 = arr[i]
			if t1 == i + 1:
				arr[i] = -t1
				i += 1
			elif t1 > 0:
				if t1 > N:
					arr[i] = 0
					i += 1
					continue
				t2 = arr[t1 - 1]
				if t2 == t1:
					arr[i] = 0
					arr[t1 - 1] = -t1 - t2
					i += 1
				elif t2 < 0:
					arr[i] = 0
					arr[t1 - 1] -= t1
					i += 1
				else:
					if t2 <= N:
						arr[i] = t2
					else:
						arr[i] = 0
					arr[t1 - 1] = -t1
			else:
				i += 1
		for i in range(N):
			arr[i] = arr[i] // -(i + 1)
		return arr


from collections import Counter

class Solution:

	def frequencyCount(self, arr, N, P):
		a = Counter(arr)
		for i in range(len(arr)):
			arr[i] = a.get(i + 1, 0)
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		i = 0
		while i < N:
			if arr[i] <= 0 or arr[i] > N or arr[i] > P:
				i += 1
				continue
			elemIndex = arr[i] - 1
			if arr[elemIndex] > 0:
				arr[i] = arr[elemIndex]
				arr[elemIndex] = -1
			else:
				arr[elemIndex] -= 1
				arr[i] = 0
				i += 1
		for i in range(N):
			if arr[i] < 0:
				arr[i] = -arr[i]
			else:
				arr[i] = 0


class Solution:

	def frequencyCount(self, arr, N, P):
		a = [0 for i in range(N + 1)]
		for x in arr:
			if x <= N and x > 0:
				a[x] += 1
		for i in range(N):
			arr[i] = a[i + 1]


class Solution:

	def frequencyCount(self, arr, N, P):
		temp = {}
		for i in arr:
			temp[i] = temp.get(i, 0) + 1
		for j in range(N):
			if j + 1 in temp:
				arr[j] = temp[j + 1]
			else:
				arr[j] = 0
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		count_first_index = 0
		i = 0
		while i < N:
			if arr[i] == 1:
				count_first_index += 1
			if arr[i] > N:
				arr[i] = 0
			else:
				arr[i] -= 1
			i += 1
		i = 0
		while i < N:
			arr[arr[i] % N] += N
			i += 1
		i = 0
		while i < N:
			arr[i] //= N
			i += 1
		arr[0] = count_first_index
		return arr


from collections import Counter

class Solution:

	def frequencyCount(self, arr, N, P):
		freq_dict = dict(Counter(arr))
		arr[:] = [freq_dict.get(i, 0) for i in range(1, N + 1)]


from collections import Counter

class Solution:

	def frequencyCount(self, arr, N, P):
		dic = {}
		c = Counter(arr)
		for i in range(1, len(arr) + 1):
			if i in c:
				dic[i] = c[i]
			else:
				dic[i] = 0
		for i in range(len(arr)):
			arr[i] = dic[i + 1]


class Solution:

	def frequencyCount(self, arr, n, P):
		for i in range(n):
			arr[i] -= 1
		for i in range(n):
			if arr[i] % P < n:
				arr[arr[i] % P] += P
		for i in range(n):
			arr[i] = arr[i] // P


class Solution:

	def frequencyCount(self, arr, N, P):
		for i in range(N):
			arr[i] = arr[i] - 1
		for i in range(N):
			if arr[i] % P < N:
				arr[arr[i] % P] += P
		for i in range(N):
			arr[i] = arr[i] // P
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		dic = {}
		for i in range(N):
			if arr[i] in dic:
				dic[arr[i]] += 1
			else:
				dic[arr[i]] = 1
		for i in range(1, N + 1):
			if i in dic:
				arr[i - 1] = dic[i]
			else:
				arr[i - 1] = 0


class Solution:

	def frequencyCount(self, arr, N, P):
		dic = {}
		for i in range(1, N + 1):
			dic[i] = 0
		for num in arr:
			if num in dic.keys():
				dic[num] += 1
		temp = list(dic.values())
		for i in range(len(arr)):
			arr[i] = temp[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		l = [0] * N
		for i in range(N):
			if arr[i] < N + 1:
				l[arr[i] - 1] += 1
			else:
				continue
		for i in range(N):
			arr[i] = l[i]
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		dict_ = dict()
		result = []
		for number in arr:
			dict_[number] = dict_.get(number, 0) + 1
		arr.clear()
		for index in range(1, N + 1):
			arr.append(dict_.get(index, 0))


class Solution:

	def frequencyCount(self, arr, N, P):
		ans = [0] * (N + 1)
		for elem in arr:
			if elem <= N:
				ans[elem] = ans[elem] + 1
		for i in range(N):
			arr[i] = ans[i + 1]


class Solution:

	def frequencyCount(self, arr, N, P):
		hmap = {}
		for val in arr:
			hmap[val] = hmap.get(val, 0) + 1
		for i in range(1, N + 1):
			if i in hmap.keys():
				arr[i - 1] = hmap[i]
			else:
				arr[i - 1] = 0
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		res = [0] * N
		for i in range(N):
			if arr[i] <= P and arr[i] <= N:
				res[arr[i] - 1] += 1
		for i in range(N):
			arr[i] = res[i]
		j = len(res)
		while len(arr) > j:
			arr1.pop()


class Solution:

	def frequencyCount(self, arr, N, P):
		a = [0] * max(P, N)
		for i in arr:
			a[i - 1] += 1
		arr[:] = a[:N]
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		d = {}
		for i in arr:
			d[i] = d.get(i, 0) + 1
		ans = []
		for i in range(1, N + 1):
			if i not in d:
				ans.append(0)
			else:
				ans.append(d[i])
		for i in range(0, N):
			arr[i] = ans[i]


class Solution:

	def frequencyCount(self, arr, N, P):
		hashmap = dict()
		for i in range(N):
			if arr[i] in hashmap:
				hashmap[arr[i]] += 1
			else:
				hashmap[arr[i]] = 1
		count = 1
		for i in range(N):
			if count <= P and count in hashmap:
				arr[i] = hashmap[count]
				count += 1
			else:
				arr[i] = 0
				count += 1


class Solution:

	def frequencyCount(self, arr, N, P):
		hash_map = dict()
		for num in arr:
			hash_map[num] = hash_map.get(num, 0) + 1
		for i in range(len(arr)):
			arr[i] = hash_map.get(i + 1, 0)


class Solution:

	def frequencyCount(self, arr, N, P):
		for i in range(N):
			if arr[i] % 10000000 <= min(N, P) and arr[i] % 10000000 > 0:
				arr[arr[i] % 10000000 - 1] += 10000000
		for i in range(N):
			arr[i] = arr[i] // 10000000


class Solution:

	def frequencyCount(self, arr, n, p):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in range(0, n):
			if i + 1 not in d:
				arr[i] = 0
			else:
				arr[i] = d[i + 1]


class Solution:

	def frequencyCount(self, arr, N, P):
		hashmap = {}
		for i in range(len(arr)):
			if arr[i] in hashmap:
				hashmap[arr[i]] += 1
			else:
				hashmap[arr[i]] = 1
		for i in range(1, len(arr) + 1):
			arr[i - 1] = 0
			if i in hashmap:
				arr[i - 1] += hashmap[i]
		return arr


class Solution:

	def frequencyCount(self, arr, N, P):
		d = dict()
		for x in arr:
			if x in d:
				d[x] += 1
			else:
				d[x] = 1
		for i in range(1, N + 1):
			if i in d:
				arr[i - 1] = d[i]
			else:
				arr[i - 1] = 0
		return arr