0

I have an array :

$arrData = array(3,5,5,8,7,6,3,1,2,8,9);

Without using any array defualt function, i want to get the count of the array...

Condition:

Duplicate array values must be considered as 1 value.

Please provide your logic that could help to find my answer without array functions...

Thanks in advance...

3
  • What have you tried ? What are your results ? What is bothering you ? Posting one's assignment on StackOverflow hoping that someone will do them for you is not well considered, you have to at least try something. Commented Sep 30, 2014 at 6:00
  • We don't do homework. Commented Sep 30, 2014 at 6:00
  • Actually it was a question asked in an interview.. and i even have no idea.. i tried to search but everybody is using the functions.. so i add this question here... Commented Sep 30, 2014 at 6:04

5 Answers 5

5

If you just want to get count of array without duplicates you can do it this way:

<?php

$arrData = array(3, 5, 5, 8, 7, 6, 3, 1, 2, 8, 9);
$out = array();

foreach ($arrData as $item) {
    if (!in_array($item, $out)) {
        $out[] = $item;
    }
}


echo count ($out);

And if you want to know number of occurences you can do it this way:

<?php

$arrData = array(3,5,5,8,7,6,3,1,2,8,9);

$out = array();

foreach ($arrData as $item) {
    if (isset($out[$item])) {
        ++$out[$item];
    }
    else {
        $out[$item] = 1;
    }
}

foreach ($out as $item => $count) {
    echo $item.' '.$count."<br />";
}

echo count($out);

You simple iterate over each element using loop and create new array with keys that are values from first array and values that are number of occurrences.

Sign up to request clarification or add additional context in comments.

1 Comment

Buddy i need total count at the end.. the duplicate will be considered as 1 value.. so the count i want in result will be : 8
1

Use something like this:

<?php
    $arrData    = array(3,5,5,8,7,6,3,1,2,8,9);
    $tmpArray   = array();

    $i = 0;
    foreach($arrData as $k => $v)
    {
        if(!in_array($v, $tmpArray))
        {
            $tmpArray[$k] = $v; 
            $i++;
        }
    }

    echo "Unique array size is: ".$i;
?>

Without in_array

<?php
    $arrData    = array(3,5,5,8,7,6,3,1,2,8,9);
    $tmpArray   = array();

    foreach($arrData as $k => $v)
    {
        $tmpArray[$k] = $v; 
    }

    $i = 0;
    foreach($tmpArray as $k => $v)
    {
        $i++;
    }

    echo "Unique array size is: ".$i;
?>

2 Comments

Thanks thats really working .. but if we don't have to use in_array function? can we still get the same result @S.Pols
Yeah ofcourse, then you would get somethink like this. (see above)
1
<?php
$arrData = array(3,5,5,8,7,6,3,1,2,8,9);

foreach($arrData as $x){
  foreach($arrData as $y=>$t){
      if($x == $y){
          $array[$x] =0;
      }else{
          $array[$x] =1;

      }
   }
}
$numCount =0;
foreach($array  as $q=>$r){
   $numCount +=1;
}
echo $numCount ;
?>

Comments

0

Use this php function

$arrData = array(3,5,5,8,7,6,3,1,2,8,9);
array_count_values($arrData );
 print_r($arrData );

widtout function default for dublicat array value

$arrData = array(3, 5, 5, 8, 8, 7, 6, 3, 1, 2, 8, 9);

    $i=0;
   $valueArray='';
$newDublicatValue=array();
foreach($arrData as $key=>$value){

    if ($arrData[$key]==$valueArray){
        $newDublicatValue[]=$arrData[$key];
    }
    $valueArray=$arrData[$key];

}
print_r($newDublicatValue);

or use this function for remove dublicate value in array

$result = array_unique($arrData );
print_r($result);

widtout function default for dublicat array value

$arrData = array(3, 5, 5, 8, 8, 7, 6, 3, 1, 2, 8, 9);

    $i=0;
   $valueArray='';
$newDublicatValue=array();
foreach($arrData as $key=>$value){

    if ($arrData[$key]==$valueArray){
       unset($arrData[$i]);
    }
    $valueArray=$arrData[$i];
    $i++;

}
print_r($arrData);

1 Comment

"Please provide your logic that could help to find my answer without array functions..."
0
<?php
  $arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];    
  print_r(arr_unique($arr1)); // will print unique values
  echo "<br>";
  echo "Unique value count => ".count(arr_unique($arr1)); // Will print unique value count
  echo "<br>";
  arr_count_val($arr1); // will print array with duplicate values

  function arr_unique($arr) {
      sort($arr);
      $curr = $arr[0];
      $uni_arr[] = $arr[0];
      for($i=0; $i<count($arr);$i++){
          if($curr != $arr[$i]) {
              $uni_arr[] = $arr[$i];
              $curr = $arr[$i];
          }
      }
      return $uni_arr;
  }

  function arr_count_val($arr) {
      $uni_array = arr_unique($arr);
      $count = 0;
      foreach($uni_array as $n1) {
          foreach($arr as $n2) {
              if($n1 == $n2) {
                  $count++;
              }
          }
          echo "$n1"." => "."$count"."<br>";
          $count=0; 
      }
  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.