# (Two new ideas about an algorithm question) give you a set of strings, such as {5, 2, 3, 2, 4, 5, 1, 5}, so that you can output the one with the most occurrences and the largest number, appears several times

Source: Internet
Author: User

It is not very difficult to see an algorithm question on the Internet. There are also solutions for searching, but there are usually several layers of for loops. I tried to write it down.

`/*** Give you a set of strings such as {5, 2, 3, 2, 4, 5,}, so that you can output the maximum number of occurrences and the maximum number, appears several times * advantage: time complexity is O (n) * disadvantage: some extra space is generated, such as 6, 7, 8 will also be allocated an array space, however, you can ignore the * restriction: you need to know in advance the maximum value, or the value smaller than, to determine the length of an array in advance */public static void Method1 () {int [] datas = {,}; int [] Nums = new int [10]; // 0-9, if the maximum value in the array is 123, the array length should be greater than or equal to 124 for (INT I = 0; I <datas. length; I ++) {int n = datas [I]-0; Nums [N] ++;} int maxtime = 0; (Int I = 0; I <nums. length; I ++) {system. out. println (I + ":" + Nums [I]); If (Nums [I]> maxtime) {maxtime = Nums [I];} system. out. println ("the most" + maxtime + "times"); int num = 0; For (INT I = 0; I <nums. length; I ++) {If (maxtime = Nums [I]) {num = I ;}} system. out. println ("Maximum number of occurrences:" + maxtime + "times; and the maximum number is:" + num);}/*** give you a set of strings such as {5, 2, 3, 2, 4, 5, 1, 1, 5}. This allows you to output one of the most frequently used and the largest number. * advantage: time complexity is O (n) * disadvantage: not found yet */publi C static void method2 () {int [] datas = {5, 2, 3, 2, 4, 5, 1, 1}; Map <integer, integer> map = new hashmap <integer, integer> (); For (INT I = 0; I <datas. length; I ++) {INTEGER key = datas [I]; integer value = map. get (key); If (value! = NULL) {map. put (Key, ++ value);} else {map. put (Key, 1) ;}} int maxtime = 0; int maxtime_num = 0; set <integer> keyset = map. keyset (); iterator <integer> it = keyset. iterator (); While (it. hasnext () {INTEGER key = it. next (); integer value = map. get (key); system. out. println (Key + ":" + value); If (value> = maxtime) {maxtime = value; If (Key> maxtime_num) {maxtime_num = Key ;}} system. out. println ("Maximum number of occurrences:" + maxtime + "times; and the maximum number is:" + maxtime_num );}`

Print:

0: 0

2: 3
3:1

5: 3
6: 0
7: 0
8: 0
9: 0
Up to three
Maximum number of occurrences: 3; maximum number: 5
-----------

2: 3
3:1

5: 3
Maximum number of occurrences: 3; maximum number: 5

We can see the difference between the two.

(Two new ideas about an algorithm question) give you a set of strings, such as {5, 2, 3, 2, 4, 5, 1, 5}, so that you can output the one with the most occurrences and the largest number, appears several times

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.