Raised This Month: $ Target: $400
 0% 

Get a cell with most duplicates in an array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 10-23-2012 , 15:33   Get a cell with most duplicates in an array
Reply With Quote #1

Code:
variable[ 0 ] = 5 variable[ 1 ] = 2 variable[ 2 ] = 4 variable[ 3 ] = 5 variable[ 4 ] = 8 variable[ 5 ] = 5

5 is three times in the variable (the most), how to return that value?
__________________
Currently busy working on a very large scale anime database project.

Last edited by Backstabnoob; 10-23-2012 at 15:38.
Backstabnoob is offline
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 10-23-2012 , 15:39   Re: Get a cell with most duplicates in an array
Reply With Quote #2

loop -> for ?
__________________

Last edited by micapat; 10-23-2012 at 15:39.
micapat is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 10-23-2012 , 15:44   Re: Get a cell with most duplicates in an array
Reply With Quote #3

No, really?
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 10-23-2012 , 15:54   Re: Get a cell with most duplicates in an array
Reply With Quote #4

Yep.

Is there many values ? What are their range ? More explanations maybe ?
__________________
micapat is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 10-23-2012 , 15:56   Re: Get a cell with most duplicates in an array
Reply With Quote #5

The first post and the thread title are pretty self explanatory, there is nothing more to explain.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 10-23-2012 , 17:33   Re: Get a cell with most duplicates in an array
Reply With Quote #6

You will need to loop through the array and count the values and then determine which one appeared the most.
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 10-23-2012 , 17:49   Re: Get a cell with most duplicates in an array
Reply With Quote #7

Not tested.
PHP Code:
new array[sizeof variable];

for (new 
ijsizeof variablei++)
{
    for (
0sizeof variablej++)
    {
        if (
variable[i] == variable[j])
        {
            array[
i]++;
        }
    }
}

new 
max;

for (new 
1sizeof variablei++)
{
    if (array[
i] > array[max])
    {
        
max i;
    }
}

// The answer is... variable[max] 

Last edited by hleV; 10-23-2012 at 17:53.
hleV is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 10-24-2012 , 07:21   Re: Get a cell with most duplicates in an array
Reply With Quote #8

Thanks, will try.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 10-29-2012 , 14:41   Re: Get a cell with most duplicates in an array
Reply With Quote #9

Thanks, works extremely well. There is one more request I would like to make - can you edit it so I can define which one is returned?

For example, if the cells are like

Code:
cell[ 0 ] = 1 cell[ 1 ] = 1 cell[ 2 ] = 2 cell[ 3 ] = 2 cell[ 4 ] = 2 cell[ 5 ] = 3 cell[ 6 ] = 3 cell[ 7 ] = 3 cell[ 8 ] = 3

1 is twice, 2 is three times, 3 is four times. I would like to define which one to get of these:
Code:
function( array, ..., .order = 1 ) // this would return 3, because it has most duplicates function( array, ..., .order = 2 ) // this would return 2, because it's the second one with most duplicates function( array, ..., .order = 3 ) // this would return 1, because it's the third one with most duplicates

I'm pretty sure this means rewriting of the whole script :/ sorry about that, I'll be very grateful if someone helps me, because I don't have an idea how to do this (and heck, even how the script posted earlier works).
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
Kreation
Veteran Member
Join Date: Jan 2010
Location: Illinois
Old 10-29-2012 , 16:36   Re: Get a cell with most duplicates in an array
Reply With Quote #10

Note: You should never use something without fully understanding what it does.
__________________
Hi.

Last edited by Kreation; 10-29-2012 at 16:36. Reason: typo
Kreation is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:17.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode