-4

I got a String like this:

,10,10,10,10,10,10,10,11

How can I count how many different numbers are there?

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
DragonStyle
  • 145
  • 11
  • 1
    Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) SO is **not a free coding or code conversion or tutorial or library finding service** You also have to show that you have made some effort to solve your own problem. Does the string really start with a comma. What have you tried so far? – RiggsFolly May 14 '16 at 16:02
  • yes it starts with a comma – DragonStyle May 14 '16 at 16:03

4 Answers4

0
//remove the starting from the given string,
$input = substr($input, strpos(',', $input) + 1);

//split the string on ',' and put it in an array
$numbers = explode(',', $input);

//an array to put our unique numbers in
$counter = array();
//for each number
foreach($numbers as $number) {
  //check if it is not in the unique 'counter' array
  if(!in_array($number, $counter)) {
    //remember this unique number
    $counter[] = $number;
  }
}

//count the unique number array
$different = count($counter);
//give some output
echo "There are " . $different . " unique numbers in the given string";

the input variable should be your text, because your text starts with a ',' we strip it out of our input string

Panda
  • 2,400
  • 3
  • 25
  • 35
Bert Bijn
  • 337
  • 1
  • 9
0

You can use the explode function for making the string an array then use array_unique function on it for getting the unique number's of it. From this array array you can easily count how many of your number unique.

$str = ",10,10,10,10,10,10,10,11";

$arr = explode(",", trim($str, ","));

$arr = array_unique($arr);

print_r($arr);

Result:

Array
(
    [0] => 10
    [7] => 11
)

so now its time to count, just use count.

echo count($arr);// 2
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0
  1. explode the string on ,
  2. array_filter without callback, to remove empty matches (first comma)
  3. array_unique to remove dup values
  4. count to return the size of array_unique

$string = ",10,10,10,10,10,10,10,11";
echo count(array_unique(array_filter(explode(",", $string))));
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Care to comment the DV ? – Pedro Lobito May 14 '16 at 16:16
  • 1
    He doesn't. I've downvoted this homework assignment question and upvoted your answer. The most simple and effective here. - I'd personally used array_map(intval,...) instead of array_filter(...). – j4k3 Aug 07 '17 at 13:05
-1

Try something like this :

$string = '10, 10, 11, 11, 11, 12';

$numbers = explode(',',$string);
$counter = array();
foreach($numbers as $num) {
   $num = trim($num);
   if (!empty($num)) {
      if (!isset($counter[$num])) {
         $counter[$num]=1;
      } else {
         $counter[$num]++;
      }
   }
}

print_r($counter);

Hope this help!

Michal Przybylowicz
  • 1,558
  • 3
  • 16
  • 22