Skip to content

Instantly share code, notes, and snippets.

@mehtaparitosh
Last active August 25, 2017 11:25
Show Gist options
  • Save mehtaparitosh/5e6c545f6d67ad6b54f1300d9f0bea81 to your computer and use it in GitHub Desktop.
Save mehtaparitosh/5e6c545f6d67ad6b54f1300d9f0bea81 to your computer and use it in GitHub Desktop.
Frequency Finder
/*
* @author Paritosh Mehta
*/
import java.io.*;
import java.util.*;
public class NewClass {
public static boolean exists(ArrayList<Integer> u, int val){
int flag = 0;
for(int i=0; i<u.size(); i++) {
if(val == u.get(i))
flag = 1;
}
if(flag == 1)
return true;
else
return false;
}
public static void main(String args[])throws IOException {
Scanner sc = new Scanner(System.in);
int i,j, count=0;
//Input size of array
int size = sc.nextInt();
//Create the array
int array[] = new int[size];
//Inout elements inside the array
for(i=0; i<size; i++) {
array[i] = sc.nextInt();
}
//eclaring ArrayLts to store unique elements
//and their frequency
ArrayList<Integer> uniq=new ArrayList<Integer>();
ArrayList<Integer> freq=new ArrayList<Integer>();
//Find frequency of each element,
//and store unique elements in ArrayList
for(i=0; i<size; i++){
//Check if number lies in 'uniq'
if(exists(uniq, array[i]))
break;
for(j=0; j<size; j++){
//Find number of similar values
if(array[i] == array[j])
count++;
}
uniq.add(array[i]);
freq.add(count);
count=0;
}
//Print result
for(i=0; i<uniq.size(); i++){
System.out.println(uniq.get(i) + " " + freq.get(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment