Created
March 3, 2016 00:32
-
-
Save Viacheslav77/2084433c3930aa9d52ee to your computer and use it in GitHub Desktop.
Решить задачу подсчета повторяющихся элементов в массиве с помощью 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.*/ | |
| public class DuplicateNumbers { | |
| public static void main (String[]args){ | |
| int [] data = new int [100]; | |
| Random r = new Random(); | |
| for (int i= 0; i < data.length; i++) | |
| data [i] = r.nextInt(100); | |
| Map <Integer,Integer> rez = new HashMap <Integer,Integer>(); | |
| for (int i: data) { | |
| Integer tmp1 = i; | |
| 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... 4, Duplicate... 3 | |
| Number... 5, Duplicate... 2 | |
| Number... 8, Duplicate... 2 | |
| Number... 11, Duplicate... 2 | |
| Number... 15, Duplicate... 2 | |
| Number... 20, Duplicate... 3 | |
| Number... 22, Duplicate... 2 | |
| Number... 29, Duplicate... 2 | |
| Number... 38, Duplicate... 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment