Topic describes

Their thinking

  • Use Map data structures in JS
  • The Map data structure is first created and then iterated to determine whether the data structure contains the elements of the array. If it does not, the key-value pair is created and the value is set to 1. If it does, the value is +1
  • The Map data structure is finally traversed, returning the corresponding key if the value is 1.

The problem solving code

var singleNumber = function(nums) {
    const m = new Map(a);for(let v of nums) {
        if (m.has(v)) {
            m.set(v,m.get(v)+1);
        } else {
            m.set(v,1); }};for (let v of m) {
        if(v[1= = =1) {
            return v[0]; }}};Copy the code