js 刷题

作者: 小本YuDL | 来源:发表于2019-12-10 21:04 被阅读0次

[不断补充!!!]

1.找出数组中出现最多的元素和次数
测试用例:

let arr = [3, 5, 6, 5, 9, 8, 10, 5, 7, 7, 10, 7, 7, 7, 7, 10, 10, 10, 10, 10];
输出:70

解决方法:利用对象来统计,区别for...of 与 for...in 的使用

方法一:
function test(arr) {
    if (!arr.length) return;
    if (arr.length === 1) return arr[0];
    let res = {};
    let maxItem, maxCount = 0;
    // 遍历数组
    arr.forEach((item) => {
        res[item] ? res[item] += 1 : res[item] = 1;
    });
   // 遍历 res
    for (let i in res) {
        if (res[i] > maxCount) {
            maxCount = res[i];
            maxItem = i;
        }
    }
    return maxItem*maxCount;
}
console.log(test(arr));


方法二:
function test(arr){
  let res ={};
  arr.forEach(item=>{
    if(res[item]){
      res[item].count++;
    }
    else{
      res[item] = {
        key:item,
        count :1
      }
    }
  });
  let count =0;
  let item = 0;
  res = Object.values(res);
  for(let i of Object.keys(res)){
    if(res[i].count >count){
      count = res[i].count;
      item = res[i].key;
    }
  }
  return count*item;
}
let arr = [1,2,3,2,1,3,5,7,5,55,5,7];
test(arr);


  1. 把A集合中相同的元素统计出数量(基础)
    测试用例:
var collection = [
  "a", "a", "a",
  "e", "e", "e", "e", "e", "e", "e",
  "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h",
  "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t",
  "f", "f", "f", "f", "f", "f", "f", "f", "f",
  "c", "c", "c", "c", "c", "c", "c", "c",
  "g", "g", "g", "g", "g", "g", "g",
  "b", "b", "b", "b", "b", "b",
  "d", "d", "d", "d", "d"
];
  it("把A集合中相同的元素统计出数量", function() {
    var result = count_same_elements(collection);

    expect(result).toEqual([
      {key: "a", count: 3},
      {key: "e", count: 7},
      {key: "h", count: 11},
      {key: "t", count: 20},
      {key: "f", count: 9},
      {key: "c", count: 8},
      {key: "g", count: 7},
      {key: "b", count: 6},
      {key: "d", count: 5}
    ]);

解决方法:

function count_same_elements(collection) {
  const obj = {};
  collection.forEach(item => {
    if(obj[item]) {
      obj[item].count += 1;
    } else {
      obj[item] = {
        key: item,
        count: 1
      }
    }
  });
  return Object.keys(obj).map(item => obj[item]); //map 方法返回新数组
  或
 return Object.values(obj)
}
  1. 把A集合中相同的元素统计出数量(进阶)
    测试用例:
var collection = [
    "a", "a", "a",
    "e", "e", "e", "e", "e", "e", "e",
    "h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h",
    "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t",
    "f", "f", "f", "f", "f", "f", "f", "f", "f",
    "c", "c", "c", "c", "c", "c", "c", "c",
    "g", "g", "g", "g", "g", "g", "g",
    "b", "b", "b", "b", "b", "b",
    "d-5"
  ];
 it("把A集合中相同的元素统计出数量", function() {
  var result = count_same_elements(collection);

  expect(result).toEqual([
      {key: "a", count: 3},
      {key: "e", count: 7},
      {key: "h", count: 11},
      {key: "t", count: 20},
      {key: "f", count: 9},
      {key: "c", count: 8},
      {key: "g", count: 7},
      {key: "b", count: 6},
      {key: "d", count: 5}
    ]);

解决方法:

function count_same_elements(collection) {
  //在这里写入代码
  let obj = {};
  collection.forEach(item=>{
    if(item.indexOf('-') !== -1){
      let temp = item.slice(0,item.indexOf('-'));
      obj[temp] = {
        key:temp,
        count:parseInt(item.slice(item.indexOf('-')+1)-1)
      };
      item = temp;
    }
    if(obj[item]){
      obj[item].count++;
    }
    else {
      obj[item]= {
        key : item,
        count :1
      };
    }
  });
  return Object.keys(obj).map(item => obj[item]);
}


  1. 删除偶数位重复元素
var collection_a = [1, 2, 3, 2, 5, 6, 21, 43, 12, 5];
  var collection_b = [11, 11, 22, 11, 33, 11];

  it('第偶数个元素中,选出不重复的元素', function() {
    var result = single_element(collection_a);
    expect(result).toEqual([6, 43, 5]);
  });

  it('第偶数个元素中,选出不重复的元素', function() {
    var result = single_element(collection_b);
    expect(result).toEqual([]);
  });

解决方法:

var single_element = function(collection){
  let res = collection.filter((item,index)=>{
    return index%2 === 1;
  });
  let arr = [];
  let flag = 0;
  for(let i in res){
    if(arr.includes(res[i])){
      arr.splice(i,1);
      flag = res[i];
    }
    else{
      arr.push(res[i]);
    }
  }
  for(let i in arr){
    if(arr[i] === flag){
      arr.splice(i,1);
    }
  }
  return arr;
};

4.两大一小排序

it('两大一小排序', function() {
    var result = rank_by_two_large_one_small(collection_a);
    expect(result).toEqual([2, 3, 1, 6, 8, 4, 9, 10])
  });

解决办法:

function rank_by_two_large_one_small(collection){
  collection.sort(compare);
  collection.forEach((val, index) => {
    if (index % 3 === 0 && collection[index+2]) {
      let value = parseInt(collection.splice(index, 1).toString()); //保存删除的值
      collection.splice(index+2, 0, value); //在后移两位 插入
    }
  });
  return collection;
}
function compare(a,b){
  return a-b;
}

相关文章

  • js 刷题

    [不断补充!!!] 1.找出数组中出现最多的元素和次数测试用例: 解决方法:利用对象来统计,区别for...of ...

  • js刷leetcode水题

    刷题是一方面,另一方面是熟悉js的特性 Detect Capital Description Given a wo...

  • 刷题笔记-JS相关

    前端JS相关面试题,排名没有先后只记录 1.Promise.then值穿透 你可能会认为结果是"bar", 其实不...

  • 工作工具使用技巧及学习

    ## 1、提高js基础知识及技能: https://www.codewars.com/dashboard 刷题...

  • 【JS 逆向百例】猿人学系列 web 比赛第二题:js 混淆 -

    逆向目标 猿人学 - 反混淆刷题平台 Web 第二题:js 混淆,动态 cookie 目标:提取全部 5 页发布日...

  • 刷题刷题

    时间紧迫,任务繁重,又有疫情影响,搞的人心惶惶,一时间复习得不安宁,又舍不得摆烂。 在焦灼、惶恐的情绪中,紧张急迫...

  • 2022-09-16

    刷题,刷题还是刷题

  • [JDCTF] web writeup

    偶然看到的刷一刷 签到题~ 进入题目 既然签到题F12一下 真的发现了,有提示打开那个js 跑一下就有flag了 ...

  • 2018-07-16

    刷题,祸害的何止是教育? 报班,刷题;买练习册,刷题;家教,刷题;跟不上,刷题;学得好,刷题;为了抢跑,刷题;为了...

  • 刷题啊刷题

    因为月底又要考试,所以最近几天一直在刷题。按说是看了书再看视频再刷题效果比较好才是。可是来不及了啊。 上次考试,就...

网友评论

      本文标题:js 刷题

      本文链接:https://www.haomeiwen.com/subject/xpcngctx.html