简体   繁体   中英

How many times can x unique values be pulled from an array

I have an array of values (non unique) and I need to work out how many times I can pull out x (eg 3) unique items from that array.

eg

[5, 5, 4, 1, 2, 3, 4, 2, 1, 3, 5, 5]

What is the largest quantity of unique items of length 3 (eg [5, 4, 1] ) I can retrieve?

For context, this is for an offer system in a shopping cart. The array of values is the product ids, and I need to know how many times I can apply a specific offer that requires 3 different items from the array of ids in order to be valid.

Thanks for any help - just ask if anything is unclear and I'll try to explain. If I've missed an existing question that answers this please let me know and I'll close this question.

Here's one way you can follow:

$basket = array(5, 5, 4, 1, 2, 3, 4, 2, 1, 3, 5, 5);
$length = 3; //unique set size

function getSetCount($basket, $length) {
    $counts = array_count_values($basket); //count of each ID
    $totalCount = count($basket); //count of all elements

    //maximum amount of sets
    $max = floor($totalCount / $length);

    //since every ID can be only once in a set, it all above $max occurences won't be able to fit any set.
    $reducedCounts = array_map(function ($el) use ($max) {
        return min($el, $max);
    }, $counts);

    //final result is "all elements that should fit in sets / size of set
    return floor(array_sum($reducedCounts) / $length);
}

If you would like to print them:

function printSets($basket, $length) {
    $counts = array_count_values($basket); //count of each ID

    while(!empty($counts)) {
        arsort($counts);//reverse sort with keeping the keys
        $set = array_slice(array_keys($counts),0,$length); //get the set
        echo implode(',', $set) . '<br/>';
        foreach($set as $id) { //reduce counts
            $counts[$id]--;
        }
        $counts = array_filter($counts); //clean zeros
    }
}

The code above may not handle proparly some edge cases. But this is the idea.

Basically array_count_values() counts values' occurences and returns an array of value => count pairs. Then its easy to manipulate this data.

If i understand you correctly:

function getChunksCount($products)
{   
    // get unique ids
    $uniqueProducts = array_unique($products);

    // count unique ids
    $count = count($uniqueProducts);

    // let's get the number of chucks available
    $chunkSize = 3;

    // round to the floor
    return floor($count / $chunkSize);
}

No complex logic or processing at all. Next time try to write down what exactly needs to be done in what order, and the solution might become pretty obvious :)

You can do it using array_unique and array_slice .

$arr = [5, 5, 4, 1, 2, 3, 4, 2, 1, 3, 5, 5];

$new_arr = array_slice(array_unique($arr), 0, 3);
print_r($new_arr); //Array ( [0] => 5 [1] => 4 [2] => 1 )

You can use array_count_values .

<?php `$array = array(5, 5, 4, 1, 2, 3, 4, 2, 1, 3, 5, 5); $vals = array_count_values($array); echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>'; print_r($vals);`?>

Output is -Array ( [5] => 4 [4] => 2 1 => 2 [2] => 2 [3] => 2 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM