Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: how to find the frequency of elements in an array

  1. #1
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to find the frequency of elements in an array

    Quote Originally Posted by heman View Post
    Bro I wish to accomplish my task only by using loops and conditional statements.
    thanks for reply
    Your solution wouldn't be 'as simple as possible' ...

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: how to find the frequency of elements in an array

    Quote Originally Posted by JosAH View Post
    Your solution wouldn't be 'as simple as possible' ...

    kind regards,

    Jos
    if it is not as simple as possible then kindly provide me any solution with only loops and conditional statements.
    thanks

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default 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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: how to find the frequency of elements in an array

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default 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

  1. Sum up elements in an array?
    By D-X69 in forum New To Java
    Replies: 3
    Last Post: 05-30-2012, 04:23 PM
  2. Find two elements in an array that sum to k
    By jamalraihan in forum New To Java
    Replies: 1
    Last Post: 12-04-2011, 05:34 AM
  3. Replies: 3
    Last Post: 11-25-2011, 12:38 AM
  4. keep the first N elements of an array only
    By aneuryzma in forum New To Java
    Replies: 13
    Last Post: 03-27-2011, 04:03 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •