Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does java find most of the elements in the array

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how java finds most elements in the array". In daily operation, I believe many people have doubts about how java finds most elements in the array. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how java finds most elements in the array". Next, please follow the editor to study!

Problem: given an array of size n, find most of the elements. Most elements refer to elements that appear in an array more times than ⌊ n ⌋ 2. You can assume that the array is not empty and that there are always many elements in a given array. For example, most of the elements of the array are 3, and most of the elements of the array are 2.

Since most elements appear in the array more times than ⌊ n ⌋, sort the array, and then take the middle value, you will get most elements, otherwise it does not meet the meaning of the topic.

In this way, two lines of code can be done:

Java Arrays.sort (nums); return nums [nums.length > > 1]

But the time complexity is O (nlogn) and the space complexity is O (logn).

How do we usually vote? Everyone chose a person to write on the note, and then began to take apart the paper ball to see who was chosen. At first, everyone acquiesced to 0 votes, and then the person who voted on the note had one more vote, and finally saw who had more votes. Coming back to our topic, since it is the number of numbers, and the number of occurrences is greater than ⌊ name2 ⌋, then we can assume that a number is the required number of numbers, and set the number of occurrence of this number to 0, and then compare it with the next number. If it is the same, let's add 1 to the number of occurrences of this number, and if it is different, let the number be reduced by 1. When this value is reduced to 0, it means that the number assumed at the beginning is not the mode, then change to the current number and continue the cycle. In this way, the number of times that the last number appears must be greater than or equal to 0, otherwise it does not conform to the meaning of the topic that the number of times is greater than ⌊ nbank 2 ⌋. Finally, the real number of numbers can be returned.

Java / / set the initial number of votes to 0 int count = 0; / / first define the required mode as an empty Integer majorityElement = null; / / circular array for (int num: nums) {/ / when count is 0, assume that the current number is the required mode if (count = = 0) {majorityElement = num;} / / when num is equal to the hypothetical mode, count adds 1 count + = (num = majorityElement)? 1:-1 } / / the real mode return majorityElement is returned at last

At this point, the study of "how java finds most of the elements in the array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report