Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,787 answers

573 users

How to check if an array contain consecutive integers in Node.js

1 Answer

0 votes
function array_contain_consecutive_integers(arr) {
	if (arr.length <= 1) {
        return true;
    }
 
    let min = arr[0];
    let max = arr[0];
        
    for (const val of arr) {
        if (val < min) {
            min = val;
        }
        if (val > max) {
            max = val;
        }
    }
        
    if (max - min != arr.length - 1) {
        return false;
    }
        
    let st = new Set();
    for (const  val of arr) {
        if (st.has(val)) {
            return false;
        }
        st.add(val);
    }
    return true;
}
        
const arr = [-2, 3, 6, 0, -1, 4, 2, 5, 1];

if (array_contain_consecutive_integers(arr)) {
    console.log("Yes");
}
else {
    console.log("No");
}


  
  
  
  
/*
run:
  
Yes
  
*/

 





answered Aug 19, 2022 by avibootz

Related questions

1 answer 50 views
1 answer 48 views
1 answer 50 views
...