Results 1 to 9 of 9
- 08-23-2012, 06:21 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 35
- Rep Power
- 0
how to find the frequency of elements in an array
hiii,
I am very newbie to this world of java.
can some one please help with the code for "how to find the frequency of elements in an array".
The program should be as simple as possible as i am a newbie..
thanks.
the output should be like this:
input array: 1,2,3,4,2,3,4,5,8
digit frequency
1 1
2 2
3 2
4 2
5 1
8 1
Any kind of help is welcome.
- 08-23-2012, 06:27 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: how to find the frequency of elements in an array
Have you had a look at a SortedMap<Integer, Integer>? It makes it all so much easier (arrays are so Fortranesque).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-23-2012, 07:02 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 35
- Rep Power
- 0
Re: how to find the frequency of elements in an array
Bro I wish to accomplish my task only by using loops and conditional statements.
thanks for reply
- 08-23-2012, 09:03 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-24-2012, 05:42 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 35
- Rep Power
- 0
- 08-24-2012, 08:59 AM #6
Re: how to find the frequency of elements in an array
Go through this tutorial: Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Then post your best efforts and if you still have problems, ask a specific question.
We don't do your homework for you here, but we're more than willing to help you learn enough to do it yourself.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-24-2012, 10:41 AM #7
Member
- Join Date
- Aug 2012
- Posts
- 35
- Rep Power
- 0
Re: how to find the frequency of elements in an array
here is my code:
class Fre
{
public static void main(String ar[])
{
frequencycount(new int[]{1,2,3,3,2,1,1,1,5,6,5,8,9,6});
}
static void frequencycount(int x[])
{
int i=0,j=0;
int fr[]=new int[x.length];
for(i=0;i<fr.length;i++)
{
fr[i]=0;
}
/////calculating frequency
for(i=0;i<x.length;i++)
{
for(j=0;j<x.length;j++)
{
if(x[i]==x[j])
{
fr[i]++;
}
}
}
for(i=0;i<fr.length;i++)
{
System.out.println(" "+fr[i]);
}
}
}//class
it finds frequency of each element but does not print in a specific pattern what i wanted as i posted in my first thread.
thanks for reply.
- 08-24-2012, 01:14 PM #8
Re: how to find the frequency of elements in an array
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-24-2012, 03:04 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 35
- Rep Power
- 0
Re: how to find the frequency of elements in an array
my code:
Java Code:class Fre { public static void main(String ar[]) { frequencycount(new int[]{1,2,3,3,2,1,1,1,5,6,5,8,9,6}); } static void frequencycount(int x[]) { int i=0,j=0; int fr[]=new int[x.length]; for(i=0;i<fr.length;i++) { fr[i]=0; } /////calculating frequency for(i=0;i<x.length;i++) { for(j=0;j<x.length;j++) { if(x[i]==x[j]) { fr[i]++; } } } for(i=0;i<fr.length;i++) { System.out.println(" "+fr[i]); } } }//class
Similar Threads
-
Sum up elements in an array?
By D-X69 in forum New To JavaReplies: 3Last Post: 05-30-2012, 04:23 PM -
Find two elements in an array that sum to k
By jamalraihan in forum New To JavaReplies: 1Last Post: 12-04-2011, 05:34 AM -
searching one elements of array into another array???
By hacikho in forum New To JavaReplies: 3Last Post: 11-25-2011, 12:38 AM -
keep the first N elements of an array only
By aneuryzma in forum New To JavaReplies: 13Last Post: 03-27-2011, 04:03 PM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks