Count the number of duplicates in the two corresponding Sets

Source: Internet
Author: User

When I was in my sophomore year, I had a problem and had to worry about it. So I ran to the office and asked a professional teacher .. At that time, the teacher said that the problem was good. Let me sort out the problem .. I learned later that my problem was included in the lab content that I had to do every semester .. Later, I became a junior ,.. Some professional students just learned Java and ran to ask me, saying that the teacher asked him to ask me and that this problem was caused by me (Wow ~ At that time, it was rather proud .)... Then I am a senior .. Another student came to me .. You know ). Well, if you don't want to talk about it, you will be happy. In fact, this problem is not hard .. By the way, I think it is necessary to share it online (although I have been diving )... If you have a better way, you can join us ..

At that time, I remember that the teacher gave me the idea that the value in two maps... map is also of the map type .. However, I think it is good to use a new method ..

Ah, actually, I prefer to manipulate strings...


Problem description:

Any two sets of A and B with the same number are given. (For example, if a is {,}, B is {,}), write the program:
The correspondence between each number in a and the number at the same position in B is called, and the number of values of B corresponding to each number in a is counted: (in the preceding example, when a is 2, there are 0 and 1 Values in B, of which 0 is one and 1 is two. When a is 1, B only has the value 0, and the number of values 0 is 1 .)

Import java. util. hashmap;
Import java. util. Map;
Import java. util. Set;

Public class TT {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

Int A [] = {2, 1, 2, 2 };
Int B [] = {1, 0, 1 };


String [] STR = new string [A. Length];
For (INT I = 0; I <A. length; I ++ ){
String S = "" + A [I] + B [I];
STR [I] = s;
}

Map <string, integer> map = new hashmap <string, integer> ();
For (string S: Str ){
Map. Put (S, map. Get (S) = NULL? 1: map. Get (s) + 1 );
}

// Print
Set <string> keyset = map. keyset ();
For (string SS: keyset ){
System. Out. println ("in array a" + SS. charat (0) + "corresponds to" in array B"
+ SS. charat (1) + ", repeat count:" + map. Get (SS ));
}
}

}

Output result:
2 In array a corresponds to 1 in array B, and the number of duplicates is: 2
2 In array a corresponds to 0 in array B, and the number of duplicates is: 1
1 In array a corresponds to 0 in array B, and the number of duplicates is: 1

The content of the two Arrays can be changed as long as the two arrays have the same length .. For example:

Int A [] = {3, 4, 3, 3, 4, 3 };
Int B [] = {2, 4, 1, 1, 4, 2 };


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.