Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added Count Frequency Of Elements In Java For issue jainaman224#1601
  • Loading branch information
chanpreet1999 authored and Mrunal committed Apr 8, 2020
1 parent facf945 commit f2e071c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Freq_Of_Elements_Of_Array/CountFrequencyOfArrayElements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*Problem Statement
* Given an array of elements count the frequency of every element in the array.
*/

import java.util.HashMap;
import java.util.Scanner;

public class CountFrequencyOfArrayElements
{

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

// input size of array
int n = sc.nextInt();
// input array
int a[] = new int[n];
for(int i = 0; i < n; i++)
a[i] = sc.nextInt();

countFreq(a, n);

sc.close();
}

// function to count frequency
private static void countFreq(int[] a, int n) {
// using hashmap to store the freq
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i = 0; i < n; i++)
{
if( hm.containsKey(a[i]) )
hm.put( a[i], hm.get(a[i]) + 1);
else
hm.put(a[i], 1);
}

//display the frequencies
System.out.println("Element \t Frequency");
hm.forEach(
(k,v) ->
{
System.out.println(k + " \t\t " + v);
}
);
}

}

/*
* Input :
5
2 1 1 6 1
* Output :
Element Frequency
1 3
2 1
6 1
* */

0 comments on commit f2e071c

Please sign in to comment.