"Original" to find the number of repetitions

Source: Internet
Author: User

Students in the group sent one to the written question:

There are 100 random numbers in an int array, and there are duplicates in these numbers, so find out the number of repetitions of each of the 100 numbers.

Well, that's probably what it means.

When I thought about it, I came up with a list<map<string,int>>. The idea is to iterate through the first element of the array and put the results in the MAP

The specific operation is:

Data: int[100]={1,2,2,1,2,132,1321,1,23,2,1,2,...}

Step 1:new map<1,1>

Step 2:new map<2,1>

Step 3:find (map.key==2) then (map.value) + +

That's a thought.

Then the practice of the next, originally thought more than 10 minutes can be done, did not expect to get 1 hours. Ashamed..

public class founder {static int xx = 1;private list<map<string, integer>> mfunction (int n[]) {list<map< String, integer>> mlist = new arraylist<map<string, integer>> (); Hashmap<string, integer> mmap;int target = -1;int counter = 1; String targetkey=null, counterkey=null;for (int x:n) {if (Mlist.isempty ()) {MMap = new hashmap<string, integer> () ; Mmap.put ("Target" + x, x), Mmap.put ("counter" + x, counter); Mlist.add (MMAP);} else {Boolean flag = false;for (int i = 0;i < Mlist.size (); i++,target =-1, Targetkey = "", counterkey= "") {MMap = (has Hmap<string, integer>) mlist.get (i); Set MSet = Mmap.keyset (); Iterator it = Mset.iterator (); while (It.hasnext ()) {String temp = (string) it.next (); char C = temp.c Harat (0), if (c== ' t ') {Targetkey = Temp;counterkey = (String) it.next ();} Else{counterkey = Temp;targetkey = (String) it.next ();}} target = Mmap.get (targetkey), counter = Mmap.get (Counterkey), if (target = = x) {++counter;mmap.put ("counter" + Target, Counter); flag = true;} xx++;} if (!flag) {counter = 1;mmap = new hashmap<string, integer> (), Mmap.put ("target" + x, x); Mmap.put ("Counter" + x, Counte R); Mlist.add (MMAP);}}} return mlist;} public static void Main (string[] args) {int test1n[]={1,1,1,1,3,2,2,3,2,2,3};int test2n[] = new Int[100000];for (int i = 0; i<test2n.length;i++) {Test2n[i] = (int) (Math.random () *10);} list<map<string, integer>> mlist = new founder (). Mfunction (test2n); for (int x:test2n) {System.out.print (x+ " ");} System.out.println (); for (int i = 0;i < Mlist.size (); i++) {hashmap<string, integer> MMap = (hashmap<string, integer>) Mlist.get (i); set<string> set = Mmap.keyset ();iterator<string> it = Set.iterator (); while (It.hasnext ()) {String key = It.next (); int value = Mmap.get (key); System.out.print (key+ ":" +value+ "");} System.out.println ();} System.out.println (XX);}}

haha haha, no comments!!!

Yes, just no comments, master the idea is ok ... (In fact, it is lazy ah.) Not standard AH ... BA Ga)

Finally think about how to do the big data volume. This is actually the point where I have the idea of documenting this algorithm, if the original data is 1w,10w. 100 million ... WTF

Instantly think of the cache block hahaha, feel very fun ah, get a raw data 10% cache block, with FIFO. The LRU thing or the hit count is not an optimization.

Have time to play again, say in addition to interview questions, who TM also digital character play it.

Also, the students think of the method is the first sort, in the traversal count, seemingly OK, but to use what two points and the like to do may be faster, he said he used to bubble ... Grinning

Mrak, the AZ written question is the character string characters, similarly, the original oneself or with two for loop to engage, ashamed of shame ...

Finally, the test code are natural numbers, not tested negative, it should not be bad, also, test the amount of 10W data, finished seemingly with 90+w times traversal, mainly because the map of the keyset more and more long reason, add a cache block should be much better.

Use 2for words OMG, with bubbling words. OMG, in a two-point word. Don't know O (Nlogn)? WTF? I'll do it when the egg hurts.

You crossing, reprint instructions, Thanks

And there's a better way to shake your face!!!

"Original" to find the number of repetitions

Contact Us

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.