In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to calculate the number of javascript, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.
Javascript method: 1, create a new array; 2, count the number of times each value appears; 3, traverse the array and find the number of numbers.
This article operating environment: Windows7 system, javascript1.8.5 version, DELL G3 computer
How does javascript get the numbers?
The number of LeetCode-- JavaScript implementation
Count the number
Given an array of size n, find the multiplicity in it. The mode is an element that appears in the array more times than ⌊ n-⌋ 2.
You can assume that the array is not empty and that there is always a mode for a given array.
Example 1:
Input: [3pyrrine 2pyr3]
Output: 3
Example 2:
Input: [2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2]
Output: 2
My thoughts:
Create a new array, count the number of occurrences of each value, and then traverse the array to find the number.
Const majorityElement = function (nums) {let arry = [] for (let i in nums) {if (! arry [nums [I]]) {arry [nums [I]] =!! arry [nums [I] + 1} else {arry [nums [I] + +}} for (let i in arry) {if (nums [I] > nums) .length / 2) {return I}
The fastest solution to leetcode:
Ideas for solving the problem:
With a counter and an intermediate value, first let the intermediate value equal to the first bit of the array, in the process of traversal, let the counter increase one when you encounter the same, decrease the difference by one, and change it to the current traversal value when it is reduced to 0. when the traversal is complete, the intermediate value is returned, that is, the number. [recommended study: "js basic course"]
It feels wonderful, and there is an idea of offsetting in it.
Var majorityElement = function (nums) {let count = 0; let majority = nums [0]; for (let I = 0; I < nums.length; iTunes +) {if (count = 0) {majority = nums [I];} if (majority = nums [I]) {count++;} else {count-- }} return majority;}; Thank you for reading this article carefully. I hope the article "how to count javascript" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.