import java.io.*;
public class OccurancesInArray{
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many elements you want to enter in the array: ");
int num=0;
try{
num = Integer.parseInt(in.readLine());
}
catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a number!");
System.exit(0);
}
String[] elements = new String[num];
int a;
int k;
for(int i = 0; i < num; i++){
elements[i] = in.readLine();
}
for(int i = 0; i < elements.length; i++){
a = 0;
k = 1;
for(int j = 0; j < elements.length; j++){
if(j >= i){
if(elements[i].equals(elements[j]) && j != i){
k++;
}
}
else if(elements[i].equals(elements[j])){
a = 1;
}
}
if(a != 1){
System.out.println("Occurance of \'" + elements[i] + "\' : " + k);
}
}
}
}
Monday, March 12, 2012
OccurancesInArray
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment