Did not expect the weekend but more relaxed, sleep until the afternoon to get up, and then watch the Wolf uncle hosted the Node subway 10th period; I didn’t expect that time passed so quickly that it was seven o ‘clock. What did you learn today? I seem to remember some concepts again, I feel that every lecturer is very good, if they accept students, I can’t wait to pay homage to the introduction. The two most impressive points: Zhang Yuang single-handedly opened the strongest SSR framework in the universe in 1997; Hangzhou to buy a house price doubled does it mean millions hit the head? I, on the other hand, could be cut off next month… Alas, continue today’s problem!

Topic describes

classification difficulty 👍 👎
algorithm Simple (70.95%) 1728
The label

Hash table
An operation

The company

airbnb | palantir

Given an array of non-empty integers, each element appears twice except for one element. Find the element that appears only once.

Description:

Your algorithm should have linear time complexity. Can you do it without using extra space?

Example 1:

Input: [2,2,1] output: 1Copy the code

Example 2:

Input: [4,1,2, 2] Output: 4Copy the code

Thought analysis

At first glance, this problem is familiar, and yesterday’s “there are duplicate elements” similar to the violent solution, first sort and then compare, if the preceding and the following element is not equal to return the element.

AC code

Violent solution

/** @lc app=leetcode.cn id=136 lang=javascript ** [136] Number that only appears once */ / @lc code=start /** * @param {number[]} nums * @return {number} */ var singleNumber = function (nums) { let arr = nums.sort(); for (let i = 0; i < arr.length; i++) { if (arr[i] ! = arr[i + 1]) { return arr[i]; }}}; // @lc code=endCopy the code

To be added

conclusion

Do six questions still feel very confused, can not find a way, how to get rid of Copy siege lion, has been in my mind no solution to the problem.

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign