JavaScript
x
<script>
// Javascript program to check if all array elements are // same or not.
JavaScript
function areSame(arr)
{
// Put all array elements in a HashSet
let s = new Set(arr);
// If all elements are same, size of
// HashSet should be 1. As HashSet contains only distinct values.
return (s.size == 1);
}
// Driver code
let arr=[1, 2, 3, 2];
if (areSame(arr))
document.write("All Elements are Same");
else
document.write("Not all Elements are Same");
// This code is contributed by patel2127
Advertisement
Answer
Taking the information from the question previously dealt with here:
Check if all values in array are the same
I think your function would look like this:
JavaScript
function areSame(array $arr) : bool
{
return(count(array_unique($arr)) === 1);
}
array_unique() returns the same array with all duplicates removed and count() counts the number of elements in an array.
If the array $arr only has one value after all duplicates are removed then all the values in the original array are unique.