Can you please help me to understand this code?

// C++ implementation of above approach 
#include <bits/stdc++.h> 
using namespace std; 

void printRepeating(int arr[], int size) 
{ 
	int *count = new int[sizeof(int)*(size - 2)]; 
	int i; 
		
	cout << " Repeating elements are "; 
	for(i = 0; i < size; i++) 
	{ 
		if(count[arr[i]] == 1) 
			cout << arr[i] << " "; 
		else
			count[arr[i]]++; 
	} 
} 

// Driver code 
int main() 
{ 
	int arr[] = {4, 2, 4, 5, 2, 3, 1}; 
	int arr_size = sizeof(arr)/sizeof(arr[0]); 
	printRepeating(arr, arr_size); 
	return 0; 
} 

Here i cant understand how this this count* is working is how is it pointing to 1

1 Like

can u explain how is it giving the count of the elements in the array?

line ~16 count[arr[i]]++;

Of course initializing an array wouldn’t immediately set it up with the counts of each element

1 Like

thanks i understood it :grin: