banner



How To Find The Size Of An Array In Java

Frequency of Element in Java

Here, on this page, nosotros will discuss the programme to find the frequency of elements in  Coffee programming language. We are given an array and need to print the frequency of each given element.

Frequency of element in Java

Java program to find the frequency of each element in the array

Methods Discussed are :

Objective: Java Program to find the Frequency of each element in the Assortment.

  • Method 1 : Using Naive Approach with actress space.
  • Method two : Naive way without extra infinite.
  • Method three : Using Sorting
  • Method 4 : Using hash Map

Let'south hash out each method one past one,

Method i :

In this method we will count the frequency of each elements using ii for loops.

  • To bank check the condition of visited elements create a assortment of size n.
  • Run a loop from alphabetize 0 to n and check if (visited[i]==i) then skip that element.
  • Otherwise create a variable count = one to keep the count of frequency.
  • Run a loop from alphabetize i+1 to due north
  • Check if(arr[i]==arr[j]), so increment the count past 1 and ready visited[j]=1.
  • After consummate iteration of for loop print element along with value of count.

Time and Infinite Complexity :

  • Time Complexity : O(north2)
  • Infinite Complexity : O(n)

Method 1: Lawmaking in Coffee

Run

// Java program to observe the frequency of each element in the array
import java.util.Arrays; class Primary { public static void countFreq(int arr[], int n) { boolean visited[] = new boolean[n]; Arrays.fill(visited, false); // Traverse through array elements and // count frequencies for (int i = 0; i < n; i++) { // Skip this element if already candy if (visited[i] == true) keep; // Count frequency int count = one; for (int j = i + one; j < due north; j++) { if (arr[i] == arr[j]) { visited[j] = truthful; count++; } } System.out.println(arr[i] + " occurs " + count +" times "); } } // Driver lawmaking public static void main(String []args) { int arr[] = new int[]{10, 30, 10, 20, 10, 20, 30, 10}; int n = arr.length; countFreq(arr, n); } }

Output

ten occurs 4 times

30 occurs ii times

20 occurs 2 times

Method 2 :

In this method we will use the naive style to find the frequency of elements in the given integer array without using any extra space.

Method ii : Lawmaking in Java

Run

// Time Complication : O(northward^2) // Aux Space Complexity : O(1)  import java.lang.*;  class Main {     public static void main (String[] args) {         int[] arr = {5, 8, 5, seven, 8, 10};         int size = arr.length;         countFrequency(arr, size);     }      static void countFrequency(int[] array, int size)     {          for (int i = 0; i < size; i++){             int flag = 0;             int count = 0;              for (int j = i+i; j < size; j++){                 if (array[i] == array[j]){                     flag = one;                     break;                 }             }             // The continue keyword is used to stop the current iteration             // in a for loop (or a while loop), and continues to the next iteration             if (flag == 1)                 go along;              for (int j = 0;j<=i;j++){                 if (assortment[i] == array[j])                     count++;             }             System.out.println(array[i]+": "+count);         }     } }              

Output

five : 2
7 : 1
8 : ii
10 : one

Method iii :

In this method nosotros will sort the array then, count the frequency of the elements.

Fourth dimension and Space Complexity :

  • Time Complication : O(nlogn)
  • Space Complexity : O(1)

Method iii : Lawmaking in Java

Run

import java.lang.*; import java.util.Arrays;  class Principal {     public static void main (String[] args) {         int[] arr = {5, 8, 5, seven, 8, x};         int size = arr.length;         countFrequency(arr, size);     }      static void countFrequency(int[] arr, int n)     {          Arrays.sort(arr);          // Traverse the sorted assortment         for (int i = 0; i < north; i++)         {             int count = i;              // Motility the alphabetize ahead whenever             // you see duplicates             while (i < n - 1 && arr[i] == arr[i + 1]) {                 i++;                 count++;             }              System.out.println(arr[i] + ": " + count);               count++;         }     } }              

Output

5 : 2
7 : 1
viii : 2
x : ane

Method iv :

In this method we will count use hash-map to count the frequency of each elements.

  • Declare a hash map.
  • Start iterating over the entire array
  • If element is present in map, then increase the value of frequency by one.
  • Otherwise, insert that chemical element in map.
  • Afterward consummate iteration over array, start traversing map and print the key-value pair.

Fourth dimension and Space Complication :

  • Fourth dimension Complication : O(n)
  • Space Complexity : O(north)

Frequency of element in java

Method 4 : Code in Coffee

Run

import coffee.util.*; import java.util.Arrays;  class Master {    static void countFreq(int arr[], int n)    {       Map<Integer, Integer> mp = new HashMap<>();        // Traverse through array elements and       // count frequencies       for (int i = 0; i < north; i++)       {          if (mp.containsKey(arr[i]))          {            mp.put(arr[i], mp.get(arr[i]) + 1);          }          else          {            mp.put(arr[i], 1);          }      }      // Traverse through map and print frequencies      for (Map.Entry<Integer, Integer> entry : mp.entrySet())      {           System.out.println(entry.getKey() + " occurs " + entry.getValue()+" times ");      }   }   // Driver code   public static void principal(Cord []args)   {        int arr[] = new int[]{10, 30, x, 20, x, 20, xxx, x};        int north = arr.length;        countFreq(arr, n);   } }

Output

twenty occurs 2 times

10 occurs 4 times

thirty occurs 2 times

mportant Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Coffee | Python
  • Find 2d Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an assortment : C | C++ | Java | Python
  • Find the Smallest and largest element in an assortment : C | C++ | Java | Python
  • Summate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Coffee | Python
  • Sort commencement half in ascending order and 2nd half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Coffee | Python
  • Sorting elements of an array by frequency : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | Coffee| Python
  • Counting Distinct Elements in an Array : C | C++ | Java| Python
  • Finding  Repeating elements in an Assortment : C | C++ | Java |Python
  • Finding Not Repeating elements in an Array : C | C++ | Java |Python
  • Removing Duplicate elements from an assortment : C | C++ | Java | Python
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Coffee | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java | Python
  • Discover all Symmetric pairs in an assortment : C | C++ | Coffee | Python
  • Notice maximum product sub-array in a given array : C | C++ | Java | Python
  • Finding Arrays are disjoint or not : C | C++ | Java | Python
  • Determine Assortment is a subset of another array or not : C | C++ | Coffee | Python
  • Decide can all numbers of an array be made equal : C | C++ | Java | Python
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java | Python
  • Sort an array according to the order defined by another assortment : C | C++ | Java | Python
  • Replace each element of the array past its rank in the assortment : C | C++ | Java | Python
  • Finding equilibrium index of an assortment : C | C++ | Java| Python
  • Rotation of elements of array- left and right : C | C++ | Coffee| Python
  • Block swap algorithm for array rotation : C | C++ | Java| Python
  • Juggling algorithm for array rotation : C | C++ | Coffee | Python
  • Finding Round rotation of an array by K positions : C | C++ | Java | Python
  • Balanced Parenthesis Problem : C | C++ | Coffee | Python

How To Find The Size Of An Array In Java,

Source: https://prepinsta.com/java-program/to-find-the-frequency-of-elements-in-an-array/

Posted by: poteatfairse1974.blogspot.com

0 Response to "How To Find The Size Of An Array In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel