Frequency Array in java
?? In Java, a frequency array is a data structure used to count the occurrences of elements in an array or collection. It is commonly used when you need to analyze the distribution or frequency of elements in a dataset.
Let's say you have an array of integers, and you want to count how many times each integer appears in the array. Here's how you can create a frequency array in Java:
First, initialize an array of integers. Let's assume it's called "arr":
int[] arr = {1, 2, 1, 3, 4, 2, 1};
int[] freqArray = new int[max + 1];
Iterate over the input array and increment the corresponding index in the frequency array:
for (int i = 0; i < arr.length; i++) {
freqArray[arr[i]]++;
}
Now, the frequency array contains the counts of each element in the input array. For example, freqArray[1] will store the frequency of 1, freqArray[2] will store the frequency of 2, and so on.
?? You can access the frequencies in the frequency array to perform further analysis or computations. For instance, you can find the most frequent element, calculate the sum of frequencies, or identify any patterns in the distribution of elements.
Frequency arrays are especially useful when you want to avoid nested loops for counting elements and achieve a linear time complexity, O(n), where n is the number of elements in the input array.
Summary
?? In summary, a frequency array in Java is a data structure used to count the frequency or occurrence of elements in an array or collection. It allows you to efficiently track how many times each unique element appears in the given data. Here are the key points:
2. Initializing the Array:
and add 1 For example, if the elements are integers ranging from 0 to 100, the size of the frequency array would be 101 (0 to 100 inclusive).
3. Counting Frequencies:
领英推荐
4. Accessing Frequencies:
5. Time Complexity:
6. Space Complexity:
Frequency arrays provide an efficient way to count the frequencies of elements without the need for nested loops or additional data structures. They offer a straightforward and optimized approach to solving problems related to element frequencies in Java.
Example:
Counting the Occurrences of Each Letter
The program does the following:
Simple Run is :
d q c m n l e k o k x y h y b l a w t z l o i e p p q a m x e o m j h f w b u b k r e o z r p y t i r k a n f a r z z g z x p v u p f x u a w a j r r s h x n e v m k o a x e b y p b n w p g t i v f u
The occurrences of each letter are:
a: 7 b: 5 c: 1 d: 1 e: 6 f: 4 g: 2 h: 3 i: 3 j: 2 k: 5 l: 3 m: 4 n: 4 o: 5 p: 7 q: 2 r: 6 s: 1 t: 3 u: 4 v: 3 w: 4 x: 6 y: 4 z: 5
solution:
public class test
? ? public static void main(String[] args) {
? ? ? ? char[] randomArray = createArray(100);
? ? ? ? int[] numberOfOccurrences = countOccurrences(randomArray);
? ? ? ? printArray(randomArray);
? ? ? ? printOccurrences(numberOfOccurrences);
? ? }
? ? public static char getRandomCharacter() {
? ? ? ? return (char)('a'+(int)(('z'-'a'+1)*Math.random()));
? ? }
? ? public static char[] createArray(int length) {
? ? ? ? char[] arrayOfChars = new char[length];
? ? ? ? for (int i = 0; i < length; i++) {
? ? ? ? ? ? arrayOfChars[i] = getRandomCharacter();
? ? ? ? }
? ? ? ? return arrayOfChars;
? ? }
? ? public static int[] countOccurrences(char[] array) {
? ? ? ? int[] numberOfOccurrences = new int[26];
? ? ? ? for (char c : array) {
? ? ? ? ? ? int index = c - 'a';
? ? ? ? ? ? numberOfOccurrences[index]++;
? ? ? ? }
? ? ? ? return numberOfOccurrences;
? ? }
? ? public static void printOccurrences(int[] numberOfOccurrences) {
? ? ? ? System.out.println("The occurrences of each letter are: ");
? ? ? ? for (int i = 0; i < numberOfOccurrences.length; i++) {
? ? ? ? ? ? char c = (char) ('a' + i);
? ? ? ? ? ? System.out.printf("%2c: %d", c, numberOfOccurrences[i]);
? ? ? ? }
? ? }
? ? public static void printArray(char[] array) {
? ? ? ? for (char c : array) {
? ? ? ? ? ? System.out.print(c + " ");
? ? ? ? }
? ? ? ? System.out.println();
? ? }
}