-1
  <script>

// Javascript program to check if all array elements are // same or not.

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

1

1 Answer 1

0

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:

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.

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

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.