2

I want to find and count the pair of values within an array.

For example:

var Array = ["Apple", "Pear", "Mango", "Strawberry", "Apple", "Pear", "Orange"];

Now I want to count how many times each pair (Apple and Pear, Pear and Mango and so on...) occurs in the array. If the array has uneven pair, the last value should then be zero.

The output of the array in the example should then be:

[2,1,1,1,1]

Notice that the "Apple, Pear" occurs 2 times so then the count will be two and put in the first number of the new array.

I hope I explained good enough

8
  • 1
    great!, what is your approach? Commented Aug 29, 2018 at 16:01
  • In your code example var Array = [Apple, Pear, Mango, Strawberry, Apple, Pear, Orange]; Apple and all other entries are variables, not strings. Commented Aug 29, 2018 at 16:01
  • 1
    Also, what is a pair in your understanding? Commented Aug 29, 2018 at 16:03
  • The entries in the array are strings. And by pair I mean pair of strings like "Apple, Pear". Two strings next to each other Commented Aug 29, 2018 at 16:04
  • 1
    Again, the entries in the array in your question are not strings, they are variables. String literals must be quoted in Javascript. Commented Aug 29, 2018 at 16:26

3 Answers 3

2

You could use a hash table for counting pairs by using two adjacent values and build a key of it for counting. Then use the values as result.

var array = ['Apple', 'Pear', 'Mango', 'Strawberry', 'Apple', 'Pear', 'Orange'],
    count = {},
    result;
    
array.reduce((a, b) => {
    var key = [a, b].join('|');
    count[key] = (count[key] || 0) + 1;
    return b;
});

result = Object.values(count);

console.log(result);
console.log(count);

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

3 Comments

Thanks. How is it possible arrange oject for each element (.apple', 'pear', 'mango'...) that sorts all the elements that comes after. For example if "Pear" comes 4 times after "Apple" and Mango comes 2 times after "Apple". Then the object for "Apple" would be {pear:4, mango:2}.
@Frogtown, that is more complicates. what means in this context "after"? the next item and the following? or in sense of all following items? it looks like as a new question ... maybe you try something and ask one with data, the result and what you have tried.
Ok, I'll try some solutions and post a new question if I get stuck
0

You could use sort() taking advantage of the fact that it passes the pairs needed to the callback:

var arr = ["Apple", "Pear", "Mango", "Strawberry", "Apple", "Pear", "Orange"];

var pairs = {}

arr.sort((a,b)=>{
    (a+b in pairs)?pairs[a+b]++:pairs[a+b] = 1;
})

console.log(Object.values(pairs))

4 Comments

adding this pairs[a+b] = (pairs[a+b] || 0) + 1 as NinaScholz answer inside sort makes it shorter.
Works like a charm! Thank you
sort may not implemented always in the same style. the order is not granted.
@NinaScholz I know, like for in loop, it is not necessarily stable, but see this ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort "elements that compare equal do not necessarily remain in their original order" I understand that in the result, elements that compare equal could not remain in the same order, in this case, as others, we are interested in the params, not in the order itself, so it does not matter the remain order. Also I've never found an evidence that shows a counter example. Either way thanks for the feedback.
0

This is the simplest way to do it. It is similar to the answer given by @Nina Scholz above, but without having to join or anything like that. Also note that, while it defaults to pairs (i.e., 2), it is generic enough to check for triples, quadruples, and so on:

function CountMultiples(inputArray, quantity = 2) {
    var count = {};

    return inputArray.reduce((acc, val) => {
            count[val] = (count[val] || 0) + 1;

            return count[val] % quantity > 0 ? acc : ++acc;
        }, 0);
};

var a = [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 0, 0, 0, 6];
console.log(CountMultiples(a,4));//0
console.log(CountMultiples(a,3));//1
console.log(CountMultiples(a));//4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.