可以返回大多数发行专辑的年份的功能

时间:2018-12-25 05:16:49

标签: javascript algorithm data-structures javascript-objects

以下是甲壳虫乐队专辑发行年份的列表。编写一个返回年份的函数 大多数专辑发行。如果有一年,则返回一个字符串,否则返回一个数组。

var beatles_discography = {
 "Please Please Me": 1963,
 "With the Beatles": 1963,
 "A Hard Day's Night ": 1964, 
 "Beatles for Sale ": 1964, 
 "Twist and Shout ": 1964, 
 "Help ": 1965, 
 "Rubber Soul ": 1965,
 "Revolver": 1966,
 "Sgt. Pepper's Lonely Hearts Club Band": 1967,
 "Magical Mystery Tour ": 1967, 
 "The Beatles ": 1968, 
 "Yellow Submarine ": 1969 ,
 "Abbey Road": 1969, 
 "Let It Be ": 1970
}

到目前为止,我尝试过这样:-

var x = {
 "Please Please Me": 1963,
 "With the Beatles": 1963,
 "A Hard Day's Night ": 1964, 
 "Beatles for Sale ": 1964, 
 "Twist and Shout ": 1964, 
 "Help ": 1965, 
 "Rubber Soul ": 1965,
 "Revolver": 1966,
 "Sgt. Pepper's Lonely Hearts Club Band": 1967,
 "Magical Mystery Tour ": 1967, 
 "The Beatles ": 1968, 
 "Yellow Submarine ": 1969 ,
 "Abbey Road": 1969, 
 "Let It Be ": 1970
}
var y = {};

for (var key in x){
  y[x[key]] = y[x[key]] ? y[x[key]] + 1: 1;
}
var arr = Object.keys(y);
function getYear(arr){
  for (var m=0; m<arr.length -1; m++){
  if(y[arr[0]] > y[arr[1]]){
    return arr[0];
  }else{
    var temp = [];
    if(y[m] == y[m+1]){
      temp.push(arr[m],arr[m+1]);
    }
    return temp;
  }
  }
}

console.log(getYear(arr));

我对该代码的预期输出是1964,因为在列表中,我今年仅重复了3次。如果对象中也有1965次3次,那么我需要返回一个数组[1964,1965]。感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

var albums = {
    "Please Please Me": 1963,
    "With the Beatles": 1963,
    "A Hard Day's Night ": 1964,
    "Beatles for Sale ": 1964,
    "Twist and Shout ": 1964,
    "Help ": 1963,
    "Rubber Soul ": 1965,
    "Revolver": 1966,
    "Sgt. Pepper's Lonely Hearts Club Band": 1967,
    "Magical Mystery Tour ": 1967,
    "The Beatles ": 1968,
    "Yellow Submarine ": 1969,
    "Abbey Road": 1969,
    "Let It Be ": 1970
}


function getYear(albums) {
    var albumOccurrence = {};
    var max = 0;
    var res = [];
    for (var key in albums) {
        albumOccurrence[albums[key]] = albumOccurrence[albums[key]] ? albumOccurrence[albums[key]] + 1 : 1;
        if (albumOccurrence[albums[key]] > max)
            max = albumOccurrence[albums[key]];
    }
    console.log(max, albumOccurence);
    for (var occurrence in albumOccurrence) {
        if (albumOccurrence[occurrence] == max) {
            res.push(occurrence);
        }
    }
    if (res.length == 1) {
        res = res[0];
    }
    return res;
}
console.log(getYear(albums));

这是您要找的吗?

答案 1 :(得分:1)

您可以从头开始创建一个新的数据结构,将年份作为关键,然后将其作为相册数组的值。

然后,您可以遍历新的数据结构,并获取最大专辑数和与该最大值对应的键(年):

var x = {
 "Please Please Me": 1963,
 "With the Beatles": 1963,
 "A Hard Day's Night ": 1964, 
 "Beatles for Sale ": 1964, 
 "Twist and Shout ": 1964, 
 "Help ": 1965, 
 "Rubber Soul ": 1965,
 "Revolver": 1966,
 "Sgt. Pepper's Lonely Hearts Club Band": 1967,
 "Magical Mystery Tour ": 1967, 
 "The Beatles ": 1968, 
 "Yellow Submarine ": 1969 ,
 "Abbey Road": 1969, 
 "Let It Be ": 1970
};
const res = {};
for (let key in x) {
  if (!res[x[key]]){
    res[x[key]] = [];
  }
  res[x[key]].push(key);
}

let max = 0;
let maxKeys = [];
for (let key in res) {
  if (max < res[key].length) {
    max = res[key].length;
    maxKeys = [key];
  }
  else if (max === res[key].length) {
    maxKeys.push(key);
  }
}
maxKeys.forEach(key => {
  console.log('Max year:', key, '\n', 'Albums number', res[key].length, '\n', 'Albums', res[key]);
});

答案 2 :(得分:0)

您可以创建对象数组。每个对象可以有一个年份,一系列电影和电影数量。然后,在对数组进行排序时,将给出电影数量最高的对象

let data = {
  "Please Please Me": 1963,
  "With the Beatles": 1963,
  "A Hard Day's Night": 1964,
  "Beatles for Sale": 1964,
  "Twist and Shout": 1964,
  "Help": 1965,
  "Rubber Soul": 1965,
  "Revolver": 1966,
  "Sgt. Pepper's Lonely Hearts Club Band": 1967,
  "Magical Mystery Tour": 1967,
  "The Beatles": 1968,
  "Yellow Submarine": 1969,
  'Abbey Road': 1969,
  "Let It Be": 1970
}

let yearObj = []
// iterating the object and creating an array of objects
for (let keys in data) {
  // check in the array , if there exist an object which have smae year
  let checkIfKeyPresent = yearObj.findIndex(item => {
    return item.year === data[keys]
  })
  // if the year exist then just update movies array and the count
  if (checkIfKeyPresent !== -1) {
    yearObj[checkIfKeyPresent]['movieNo'] += 1;
    yearObj[checkIfKeyPresent]['movies'].push(keys)
  } else {
    // if it does not exist then create a new object with following keys and
    // push it to the main array
    yearObj.push({
      year: data[keys],
      movieNo: 1,
      movies: [keys]
    })
  }
}
let k = yearObj.sort((a, b) => {
  return b.movies.length - a.movies.length
})
console.log(yearObj[0])