0

Shortly, here is what I want :

it returns true when

 arr=[a, b, c, d, e, f] 

or

 arr=[a, b, a, b, c, d, c, e, a, b] 

they don't have to be in order.

and false when :

 arr=[a, a, b, c, d, d, e, f] 

Thank you.

11
  • 3
    Now what you want?? Commented Dec 25, 2019 at 10:25
  • I want every next element is different than the previous one, though they can duplicate, but not next to each other.. I think the examples are clear.. Commented Dec 25, 2019 at 10:28
  • Is this really a duplicate to that question??? Commented Dec 25, 2019 at 10:34
  • @tsh I don't think so either. OP doesn't want to "jumble" the array, they want to check for consecutive repetitions and return a boolean. Why do some people have such quick trigger-fingers? Commented Dec 25, 2019 at 10:35
  • Maybe a => a.every((n, i) => i === 0 || n !== a[i - 1]) Commented Dec 25, 2019 at 10:36

2 Answers 2

1

You can use Array.prototype.every to test whether all elements in an array pass the test (that is a callback function returning a boolean value). In this case, make sure that the current element being checked is not the same as the previous one (index - 1), ignoring the very first element (!index).

var arr1 = ["a", "b", "c", "d", "e", "f"]; var arr2 = ["a", "a", "b", "c", "d", "d", "e", "f"]; function arrayHasConsecitiveRepetition(arrayIn) { return arrayIn.every(function(element, index) { return !index || element !== arrayIn[index - 1]; }); } console.log(arrayHasConsecitiveRepetition(arr1)); // true console.log(arrayHasConsecitiveRepetition(arr2)); // false

You can of course also do it the other way around, using Array.prototype.some to check if at least one element matches the condition, which might actually be better in terms of performance, as it stops evaluating once the condition is true*:

var arr1 = ["a", "b", "c", "d", "e", "f"]; var arr2 = ["a", "a", "b", "c", "d", "d", "e", "f"]; function arrayHasConsecitiveRepetition(arrayIn) { return !arrayIn.some(function(element, index) { return index && element === arrayIn[index - 1]; }); } console.log(arrayHasConsecitiveRepetition(arr1)); // true console.log(arrayHasConsecitiveRepetition(arr2)); // false

*Actually, it turns out that both methods return immediately once the condition is (not) met, so it's more a matter of taste or style than a matter of performance. I'd prefer to use the first approach in this case as it's clearer to me.

From the MDN pages:

If such an element is found, the every method immediately returns false.

If such an element is found, some() immediately returns true.

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

7 Comments

"which might actually be better in terms of performance, as it stops evaluating once the condition is true" Won't Array#every stop evaluating once the condition is false?
That is indeed a good question, I hadn't thought of that. And it turns out you're right: If such an element is found, the every method immediately returns false.! So I guess it's not a matter of performance at all, but a matter of taste.
Updated the answer to address that
so the .some and .every saves the time of making an if.. else ? right? I'm a newbie so I'm trying to understand what I'm reading and typing... Also if you don't mind, why wouldn't the answer of Monika work? that was the very first thing that came up to my mind... Thanks
It helps keep the code shorter. You could also loop over the array elements and check with if/else, yes.
|
0

One option:

const arr1 = ['a', 'b', 'c', 'd', 'e', 'f']; const arr2 = ['a', 'a', 'b', 'c', 'd', 'd', 'e', 'f']; function checkArr(arr) { let previous; const check = !arr.some(letter => { if (letter === previous) { return true; } previous = letter; }); console.log(check); } checkArr(arr1); // true checkArr(arr2); // false

1 Comment

Note that when the first element is undefined, it will return false even if the second element is not undefined. You'd probably want to check for previous && letter === previous

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.