Giter Site home page Giter Site logo

interview-question's People

Contributors

kkxujq avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

interview-question's Issues

第2题:实现一个 pipe 函数,接收若干个 function,使得前一个函数的调用结果是后一个函数的参数,最后返回一个 function

实现一个 pipe 函数,接收若干个 function,使得前一个函数的调用结果是后一个函数的参数,最后返回一个 function

/****** 用例 1:******/
const process = pipe(JSON.stringify, console.log);
process({ a: 1 });

// {"a": 1}

/******  用例 2:******/
const process = pipe(Math.max, x => x ** 2 );
process(3, 4);

// 16
function pipe(...restFns) {
  const tag = Symbol('tag');

  return (...args) => {
    return restFns.reduce((pre, cur) => {
      if (pre[tag]) return cur(...pre.args);
      else return cur(pre);
    }, { args, [tag]: true });
  };
}

// 如果入参只有一个,则以上函数简化如下
const pipe = (...resFns) => arg => resFns.reduce((pre, cur) => cur(pre), arg);

第1题:给定一个非空整数数组,找出只出现过一次的元素?

给定一个非空整数数组,找出只出现过一次的元素,比如:

输入:[1, 2, 3, 4, 1, 2, 3] 输出:[3]

function receive(arr = []) {
  const loopSet = new Set();
  const cacheSet = new Set();

  for (let len = arr.length; len --; ) {
    const cur = arr[len];

    if (!loopSet.has(cur)) {	// 如果 set 中不存在说明第一次出现
      loopSet.add(cur);		// 第一次出现则两个 set 中都记录
      cacheSet.add(cur);
    } else {					// 说明再次出现,从 cacheSet 中删除
      cacheSet.delete(cur);
    }
  }

  return Array.from(cacheSet);
} 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.