logo CodeStepByStep logo


Language/Type: PHP arrays traversals

Write a function named count_unique that accepts an array of integers as a parameter and returns a count of the number of unique values that occur in the array. If the array contains multiple occurrences of the same element value, only one of those occurrences should count toward your total.

For example, if an array named $numbers stores [7, 7, 2, 2, 1, 2, 2, 7], the call of count_unique($numbers) should return 3 because there are 3 unique values: 7, 2, and 1.


  • In solving this problem, do not create any other data structures such as arrays, strings, etc., though you may create as many simple variables (e.g. integers) as you like.
  • Do not modify the array passed to your function as the parameter, such as by sorting or rearranging its element values.
Type your PHP solution code here:

This is a function exercise. Write a PHP function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.