In a sample exercise, I was to check if all the elements in an array are identical to each other. THIS QUESTION IS NOT ABOUT THE MOST EFFICIENT WAY TO DO THIS. Rather, it is about these two solutions.
for(var i=0; i < set.length-1; i++)
{
if (set[i] != set[i+1]) // could have compared all elements to the firstelement instead of switching
{
isTrue=false;
}
}
This above algorithm compares each index to the index afterward.
var firstIndex=set[0];
for(var i=0; i < set.length-1; i++)
{
if(set[i] != firstIndex)
{
isTrue=false;
}
}
While this algorithm compares the current index to the first index. Although these algorithms are at least O(N). Does the difference in the comparisons affect the time/space complexity?