1

I'm building a Tinder like app in Angular. When the user swipes right, I am grabbing information about that object. All the images can be part of one of 6 collections.

The user can only swipe right 4 times. Which means I have 4 scenarios. What I'm looking to do is count the number of instances of all of the collections that were swiped right. I hope that makes sense.

So for instance if I end up with an array like this:

[a, b, a, d]

The winner will be A. I'm not quite sure how to count all of those and determine A as the winner.

Any and all help is appreciated!

lz430
  • 135
  • 1
  • 9
  • Please set up a jsfiddle or codepen of what you currently have so far –  Dec 07 '16 at 22:19
  • And consider rephrasing a bit. It's really unclear (at least to me) what you are trying to accomplish. – Jordan Soltman Dec 07 '16 at 22:19
  • 1
    Possible duplicate of [How to find the number of equal elements in javascript array](http://stackoverflow.com/questions/32032001/how-to-find-the-number-of-equal-elements-in-javascript-array) – nicovank Dec 07 '16 at 22:21
  • Thanks for the feedback. [link](http://430designs.com/xperience/black-label-app/deck.php). The file you're looking for is controller.js. I have an array of 4 elements. I need to see which one occurs the most. That's about as simple as I can get it – lz430 Dec 07 '16 at 22:25
  • Please [edit] your question to provide all relevant code in an [mcve] in the question itself, not on a third-party site. – Heretic Monkey Dec 07 '16 at 22:33

3 Answers3

1
const input = ['a', 'b', 'a', 'd'];

const findMostFrequentItem = arr => {
  let occurences = {};
  arr.forEach(x => occurences[x] = occurences[x] ? occurences[x] + 1 : 1);
  return Object.keys(occurences).reduce((a, b) => occurences[a] > occurences[b] ? a : b );
};

console.log(findMostFrequentItem(input));
manonthemat
  • 6,101
  • 1
  • 24
  • 49
1

In ES6 with some minor help from underscore, you can do the following:

var swipes = ["a", "b", "a", "d"];

// Calculate and store the frequency of each swipe
var frequency = swipes.reduce(function(frequency, swipe){
   var sofar = frequency[swipe];
   if(!sofar){
     frequency[swipe] = 1;
   }
   else {
     frequency[swipe] = frequency[swipe] + 1;
   }

   return frequency;
}, {}); // {} - start out with an empty frequency object

var max = Math.max.apply(null, Object.values(frequency)); // most frequent

// find key for the most frequent value
// Using underscore...
// var winner = _.findKey(frequency, val => val === max); 

// Without underscore
var winner = Object.keys(frequency).find(element => frequency[element] == max);
Kalman
  • 8,001
  • 1
  • 27
  • 45
  • I like this solution, however I'm using jquery and Angular. I don't have underscore in my project. Any chance I can do it without underscore? – lz430 Dec 08 '16 at 15:50
  • Decided to bring Underscore into the project. Worked perfectly!! Thanks so much – lz430 Dec 08 '16 at 18:13
  • 1
    I updated my answer without relying on underscore as well. Enjoy! :) – Kalman Dec 08 '16 at 18:18
0

Try something like this:

function countNum(array, num){
    var count = 0;
    for(var i = 0; i < array.length; ++i){
        if(array[i] == num)
            count++;
    }
    return count
}

And then:

var a, b, c, d;
//other stuff

min = [0, 0]
arr = [a, b, c, d]
for (index in arr){
    c = countNum(array, arr[index])
    if (c > min[0]) {
        min = [c, arr[index]]
    }
}

jsfiddle here

rassar
  • 5,412
  • 3
  • 25
  • 41