-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreq.java
48 lines (47 loc) · 1.04 KB
/
freq.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
public class freq
{
public static void main(String[] args)
{
try
{
Scanner s=new Scanner(System.in);
int [] arr;
int n,k;
System.out.print("Enter the no. of element: ");
n= s.nextInt();
arr = new int[n];
System.out.print("Enter elements: ");
for(k=0;k<n;k++)
{
arr[k]=s.nextInt();
}
int [] fr = new int [arr.length];
int visited = -1;
for(int i = 0; i < arr.length; i++){ int count = 1;
for(int j = i+1; j < arr.length; j++)
{
if(arr[i] == arr[j])
{
count++;
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
System.out.println("---------------------------------------");
System.out.println(" Element | Frequency");
System.out.println("---------------------------------------");
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.println(" " + arr[i] + " | " + fr[i]);
}
System.out.println("----------------------------------------");
}
catch(Exception e)
{
System.out.println("Due to string Exception");
}
}
}