SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member John McL's Avatar
    Join Date
    Oct 2004
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help finding most popular value in array

    I have a list of 30 single values in an array, all 4 digit numbers.

    I would like to be able to find the most popular value in this array and the number of times it occurs.

    Can anyone give me a simple way of doing this?

    Thanks,

    John

  2. #2
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    595
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    1. Pass through the array, building another array that contains the 4 digit numbers, and as a second dimension, the number of times the 4 digit number is found.

    Basically, use the 4 digit number to search your second array, if you find the 4 digit number, add 1, if you don't find it, add it as a new entry with a value of '1' in the second dimension.

    2. Then pass through the second array, you simply store the 4 digit number that has the highest value.

    Peter

  3. #3
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php has a built in for just that http://www.php.net/array_count_values


Bookmarks

Posting Permissions

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