{"size": 2242, "ext": "js", "max_stars_repo_path": "node-practice/object-keys-values/maxOccurValues.twoKeys.js", "max_stars_repo_name": "loggar/node", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node-practice/object-keys-values/maxOccurValues.twoKeys.js", "max_issues_repo_name": "loggar/node", "max_issues_repo_issues_event_min_datetime": "2020-09-09T02:11:31.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-15T14:37:51.000Z", "max_forks_repo_path": "node-practice/object-keys-values/maxOccurValues.twoKeys.js", "max_forks_repo_name": "loggar/node", "max_forks_repo_forks_event_min_datetime": "2020-11-05T08:17:44.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-05T08:17:44.000Z"}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": 1, "avg_line_length": 29.5, "max_line_length": 76, "alphanum_fraction": 0.5811775201}
/*
You are given a collection of person objects (in a natural manner for the
language you're writing in) with three fields:

{firstName, lastName, age} e.g. {Jane, Smith, 14}

We want to know which family has the longest line of "juniors". 
A family has "juniors" when multiple family members have the same full name.

Consider the following example.
[
    {"firstName": "John",  "lastName": "Doe",   "age": 13},
    {"firstName": "John",  "lastName": "Doe",   "age": 32},
    {"firstName": "John",  "lastName": "Doe",   "age": 62},
    {"firstName": "Janet", "lastName": "Doe",   "age": 14},
    {"firstName": "Jenny", "lastName": "Smith", "age": 34},
    {"firstName": "Jenny", "lastName": "Smith", "age": 12},
]

In this example, the Doe family has the longest line of juniors.

Let’s talk about your plan of attack before you start coding, and remember 
to keep talking me through what you are doing as you code.

*/

/**
 * https://codeinterview.io/--
 * @param {*} arr 
 * @returns 
 */
function maxOccurLastNames(arr = []) {
  if(!arr.length) return [];
  
  const a = []; /* firstName, lastName, cnt */
  
  arr.forEach(item => {
    let comExi = false;
    for(let i=0; i<a.length; i++) {
      if(a[i].firstName === item.firstName 
        && a[i].lastName === item.lastName
      ) {
        comExi = true;
        a[i].cnt++;
        break;
      }
    }
    if(!comExi) {
      a.push({
        firstName: item.firstName,
        lastName: item.lastName,
        cnt: 1
      });
    }
  });
  
  const maxCnt = Math.max(...a.map(item => item.cnt));
  const maxOccurComb = a.filter(item => item.cnt === maxCnt);
  
  return maxOccurComb.map(item => item.lastName);
}


test("maxOccurLastNames returns ['Doe']", () => {
  expect(maxOccurLastNames(false)).toEqual([]);
  expect(maxOccurLastNames([])).toEqual([]);
  expect(maxOccurLastNames([
    {"firstName": "John",  "lastName": "Doe",   "age": 13},
    {"firstName": "John",  "lastName": "Doe",   "age": 32},
    {"firstName": "John",  "lastName": "Doe",   "age": 62},
    {"firstName": "Janet", "lastName": "Doe",   "age": 14},
    {"firstName": "Jenny", "lastName": "Smith", "age": 34},
    {"firstName": "Jenny", "lastName": "Smith", "age": 12},
  ])).toEqual(['Doe']);
});
