Last active
March 3, 2016 00:37
-
-
Save Viacheslav77/10651ed9a6b5269855e9 to your computer and use it in GitHub Desktop.
Решить задачу подсчета повторяющихся элементов в HashMap с помощью HashMap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.Map.Entry; | |
| import java.util.Random; | |
| //Решить задачу подсчета повторяющихся элементов в HashMap с помощью HashMap. | |
| public class DuplicateNumbers { | |
| public static void main (String[]args){ | |
| Map <Integer,Integer> dn = new HashMap <Integer,Integer>(); | |
| Random r = new Random(); | |
| int tmpI = 10 + r.nextInt(100); | |
| for (int i= 0; i < tmpI; i++) | |
| dn.put(r.nextInt(100), r.nextInt(100)); | |
| Map <Integer,Integer> rez = new HashMap <Integer,Integer>(); | |
| for (Map.Entry<Integer,Integer> entry : dn.entrySet()) { | |
| Integer tmp1 = entry.getValue(); | |
| if(dn.containsValue(tmp1)){ | |
| if(rez.containsKey(tmp1)){ | |
| int tmp2 = rez.get(tmp1)+1; | |
| rez.put(tmp1, tmp2); | |
| } | |
| else | |
| rez.put(tmp1, 0); | |
| } | |
| } | |
| for (Map.Entry<Integer,Integer> entry : rez.entrySet()) { | |
| if(!entry.getValue().equals(0)) | |
| System.out.printf("Number... %s, Duplicate... %s\r\n", entry.getKey(), entry.getValue()+1); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Number... 91, Duplicate... 2 | |
| Number... 33, Duplicate... 2 | |
| Number... 97, Duplicate... 2 | |
| Number... 34, Duplicate... 2 | |
| Number... 35, Duplicate... 2 | |
| Number... 41, Duplicate... 4 | |
| Number... 47, Duplicate... 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment