I have an array and i would like to find all neighboring matches and count them, then return the count, performed in a loop like .map() so it wont be necessary to store a memory of what was counted beyond the current element. i need to use this number of current elements to reserve enough spaces for each group of elements
array = [ball, batter, batter, amount, amount, github, github, github, account, account, account, account, account, account, account, github, github, github]
example of desired results from this array would be: on first loop would return 1, second loop would return 2, then 2, then 3, then 7, then 3
and this count would be used as a variable to reserve space, something like
number to reserve: count
so through each loop, the variable count
would be changed and updated to the current elements count, and the loop that counts would not stop until the next element is not a match for the current element, and the variable count
will also not be available for use until all concurrent matches are found, so if i put console.log(count)
at the end of the function i would get each number output individually
CodePudding user response:
You can easily count the consecutive string in an array as:
const array = ["ball", "batter", "batter", "amount", "amount", "github", "github", "github", "account", "account", "account", "account", "account", "account", "account", "github", "github", "github"]
const result = [];
let last = array[0], lastSameIndex = 0;
for (let i = 1; i < array.length; i) {
if (array[i] !== last) {
result.push(i - lastSameIndex);
lastSameIndex = i;
last = array[i];
}
}
result.push(array.length - lastSameIndex);
console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Let's declare some initial values as
last = array[0]
, so last = "ball"
, and lastSameIndex = 0
So we have to start from the index 1
and we only have to consider a single case where the two elements are not equal i.e. array[i] !== last
. If they are not equal then we have to push the count of same element till now as:
result.push(i - lastSameIndex);
Now, we have to update the new value and index i.e. last
element from where we have to count that element again.
lastSameIndex = i;
last = array[i];
So far so good, but we also have to handle a case where the loop ends, In this case we have to push the count of last element from where the count started i.e. last
started or lastSameIndex
till the last element.
result.push(array.length - lastSameIndex);
CodePudding user response:
I'd store it all to an object.
So loop each item, if it's not in the results object then count and add it.
const items = ["ball", "batter", "batter", "amount", "amount", "github", "github", "github", "account", "account", "account", "account", "account", "account", "account", "github", "github", "github"]
const results = {}
items.forEach(item => {
if (!results[item]) {
results[item] = items.filter(item2 => item2 === item).length
}
})
console.log(results)
console.log(Object.values(results))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>