The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: [PHP]most common value in Array

  1. #1

    [PHP]most common value in Array

    I have searched google, here and also on php.net but not found the answer, simply how do I find the most common value in an array (mode) I know there is a way as I have done it before. its just annoying me now!

    If anyone has the answer I will be very happy again

  2. #2
    now mister or misses evildrummer you'll need to be more specific about what you're trying to achieve here because i realy have no idea what you are asking for.....

  3. #3
    You can create an associative array of each item in the array, with the key as the item in the array, and the value as the counter. Then, simply loop through the array, and if it's in the hash, increase the counter 1, otherwise set it to 1. Then, sort it based on the value, and return the last item's index.

    Here, I wrote one in Ruby:

    Code:
    def largest(array)
        # Create a hash; each key will be a unique value in a, and the value will
        # be the count. An associative array is a hash in PHP.
        count = {}
        
        # Just an iterator; it'll loop through every item in array, with the current
        # value assigned to i.
        array.each do |i|
            if count[i] # If i is already a key in count (we've already added it)
                count[i] += 1
            else # If it's not in the hash, add it, and set the value to 1.
                count[i] = 1
            end
        end
        # counts #=> {5=>6, 6=>1, 1=>3, 2=>2, 3=>2, 4=>1}
        
        # A bit more complicated. First, convert it to an array. It'll look like
        # [[5, 6], [6, 1], [1, 3], [2, 2], [3, 2], [4, 1]] at the moment, with each
        # key value pair converted to a two-item array. The first item is the key
        # in the previous hash. Now, I sort it, based on the second value in each
        # array. Then, I get the last item, and the first part of it (index 0). This
        # is the last item on the array, so it's automatically returned.
        count.to_a.sort{ |x,y| x[1]<=>y[1] }.last[0]
    end
    
    a = [1,1,1,2,2,3,3,4,5,5,5,5,5,5,6]
    puts largest(a)


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  4. #4
    Uh, there's a function to count the values of an array, Jeff. Why'd you go to all that effort?

    array_count_values

    edit:/ haha woops, i wrote county instead of count

    edit2:/ I WROTE COUNTY IN THE DESCRIPTION TOO! wth?
    Last edited by hl; February 11th, 2007 at 11:15 AM.
    got pwnt?

  5. #5
    Because I assumed he checked the docs.

    It removes the easy part, the loop. Doesn't remove the sort, though.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  6. #6
    Either way Im sure the Ruby example helped

  7. #7
    Well, I explained and used it as a proof-of-concept.

    It shows what's happening.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  8. #8

  9. #9
    Quote Originally Posted by hl View Post
    Uh, there's a function to county the values of an array, Jeff. Why'd you go to all that effort?

    array_count_values

    edit:/ haha woops, i wrote county instead of count
    I knew about that but how would I then get the value that is most common? that just counts each one.

  10. #10
    You sort it from biggest to smallest based on the values.

    arsort

    Then you've got the first element as your biggest.
    got pwnt?

  11. #11
    Quote Originally Posted by nokrev View Post
    Well, I explained and used it as a proof-of-concept.

    It shows what's happening.
    Im just teasing

  12. #12

  13. #13
    Quote Originally Posted by bwh2 View Post
    i was hoping you would add mode to that S&E thread.
    as I said in that thread I had basically all of them until my PC died now im stuck using a sony VAIO laptop

  14. #14
    Oh I made it, here it is if you want it anyone:
    PHP Code:
    <?php
    function mode($x) {
        
    $valuesarray_count_values($x);
        
    rsort($values);
        
    $mode $x[$values[0]-1];
        return 
    $mode;
    }
    ?>

  15. #15

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012