Skip to main content
edited tags
Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Tweeted twitter.com/#!/StackMma/status/554618528574484480
Source Link
Mario Krenn
  • 2.2k
  • 16
  • 35

Check whether array has only constant entries?

I have an array arr with n entries. Now I want to check whether

arr[[1]]==arr[[2]]&&arr[[2]]==arr[[3]]&&...arr[[n-1]]==arr[[n]]

Of course I could use a for-loop, like

arr = {0, 1, 0, 0};
IsConst = True;
For[i = 1, i <= Length[arr], i++,
  If[arr[[i]] != arr[[1]],
    IsConst = False;
    i = Length[arr];
  ];
];
Print[IsConst];

But that doesn't look too fast, so I wonder whether there is a faster way.