Skip to content

Instantly share code, notes, and snippets.

@anil477
Last active June 13, 2017 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/9c8ae0f8da6fd01cdd2fc621c3ca01f2 to your computer and use it in GitHub Desktop.
Save anil477/9c8ae0f8da6fd01cdd2fc621c3ca01f2 to your computer and use it in GitHub Desktop.
Count frequencies of all elements in array in O(1) extra space and O(n) time
class CountFrequency
{
// Function to find counts of all elements present in
// arr[0..n-1]. The array elements must be range from
// 1 to n
void printfrequency(int arr[], int n)
{
// Subtract 1 from every element so that the elements
// become in range from 0 to n-1
//for (int j = 0; j < n; j++)
// arr[j] = arr[j] - 1;
// Use every element arr[i] as index and add 'n' to
// element present at arr[i]%n to keep track of count of
// occurrences of arr[i]
for (int i = 0; i < n; i++){
arr[arr[i] % n] = arr[arr[i] % n] + n;
}
// To print counts, simply print the number of times n
// was added at index corresponding to every element
for (int i = 1; i < n; i++)
System.out.println(i + "->" + arr[i]/n);
}
// Driver program to test above functions
public static void main(String[] args)
{
CountFrequency count = new CountFrequency();
int arr[] = {1, 2, 2, 1, 4, 5};
int n = arr.length;
count.printfrequency(arr, n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment