简体   繁体   中英

check if atleast 4 consecutive array elements are same — javascript

Hello i have to check Arrays(all have equal lengths) which have same elements(null in mycase) in atleast 4 consecutive positions. and return true if it has so.

Example,

Arr1 = ["a","","b","c","","","","","d"]; // if array is like this it should return - True
Arr1 = ["","","","","","a","b","c","d"]; // if it is like this it should return - True
Arr1 = ["p","q","","r","","","s","",""]; // if it is like this it should return - False

Any idea?? Please Help advise or suggest somecode in the below function so to acheive this requirement in javascript

function checkForSameConsecutives(arr) {

 //somecode here 

 // if condition to return true/false;
};
checkForSameConsecutives(Arr1);

You could take a closure over a counter c and check the value and reset if the value is truthy or add one. Then return the result of the check with the wanted length.

 const check = array => (c => array.some(v => (c = v? 0: c + 1) === 4))(0); console.log(check(["a", "", "b", "c", "", "", "", "", "d"])); console.log(check(["", "", "", "", "", "a", "b", "c", "d"])); console.log(check(["p", "q", "", "r", "", "", "s", "", ""]));

Here you go:

function checkForSameConsecutives(haystack, needle, howMany) {
  let counter = 0;
  for (const el of haystack) {
    if (el === needle) {
      counter++;
      if (counter === howMany) {
        return true;
      }
    } else {
      counter = 0;
    }
  }
  return false;
}

Arguments to the function:

haystack - the array to search

needle - what are you searching for

howMany - how many hits do you need

 arr1 = ["a", "", "b", "c", "", "", "", "", "d"]; // if array is like this it should return - True arr2 = ["", "", "", "", "", "a", "b", "c", "d"]; // if it is like this it should return - True arr3 = ["p", "q", "", "r", "", "", "s", "", ""]; function hasFourOrMoreConsecutiveElements(arr) { let hasFourOrMoreConsecutiveElements = false; arr.join("-") // Replace "" with '-' and convert into string.split(/[a-zA-Z]/) // split on alphabetical characters into separate arrays -> will look like ["---", "--", "----", ...].forEach((array) => { // iterate over arrays if (array.length >= 4) { // check if any subarray has 4 or more items hasFourOrMoreConsecutiveElements= true; return; } }); return hasFourOrMoreConsecutiveElements; } console.log( hasFourOrMoreConsecutiveElements(arr1), hasFourOrMoreConsecutiveElements(arr2), hasFourOrMoreConsecutiveElements(arr3) );

Try this

 Arr1 = ["a","","b","c","","","","","d"]; // if array is like this it should return - True Arr2 = ["","","","","","a","b","c","d"]; // if it is like this it should return - True Arr3 = ["p","q","","r","","","s","",""]; // if it is like this function checkForSameConsecutives(arr) { for(let i=0; i < arr.length - 3; i+=1){ const ArrayToCheck = arr.slice(i, i+4) const ArrayToCheckSet = new Set(ArrayToCheck) if(ArrayToCheckSet.size === 1){ return true } } return false }; console.log(checkForSameConsecutives(Arr1)) console.log(checkForSameConsecutives(Arr2)) console.log(checkForSameConsecutives(Arr3))

This is not most optimised. But might give you some direction. Here code is iterating on every index and checking if next three elements are same.

I hope this following simple code help you

function checkForSameConsecutives(arr) {
            var count = 0;
            var isFound = false;
            for (var i = 1; i < arr.length; i++) {
                if (arr[i] == arr[i - 1]) {
                    count++;
                    if (count == 3) {
                        isFound = true;
                        break;
                    }
                }
            }
            return isFound;
        }

This is not be the best solution but it may help you to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM