# Find Dublicate elements in an array with the count

Page 1 of 1

## 2 Replies - 248 Views - Last Post: 31 October 2017 - 02:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407283&amp;s=b84d4f5c86c7383f7d431f4f1cf6d2b7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nihal0808

Reputation: 0
• Posts: 3
• Joined: 30-October 17

# Find Dublicate elements in an array with the count

Posted 30 October 2017 - 05:39 PM

```package Practise;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class FindDublicates {   //Class Name
public static void main(String[] args) {
FindDublicates obj = new FindDublicates();
int[] array = { 13, 3, 22, 44, 12, 13, 3, 3, 13 };  // Array declaration
obj.dublicateMethod(array);
}

public void dublicateMethod(int[] array) {  //Method
//As MAP takes Key,Value pair; after iterating through the loop,
// it checks if Key has a Value or not. If present it increments the Value
// or else it assign the Key with the number and puts Value as 1.
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
if (array.length > 1) {
for (int i = 0; i < array.length; i++) {
if (map.get(array[i]) != null) {
int count = map.get(array[i]);
map.put(array[i], count + 1);
} else
map.put(array[i], 1);
}
//The following code takes keySet and iterates using Iterator and checks for //condition
// Prints all the number who's key is greater than 1.

Set<Integer> set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Object key = it.next();
if (map.get(key) > 1) {
System.out.println("The Number is:" +key +": and repeated" +map.get(key) + "                                                                     times");
}

}
}
}
}

```

Output: The Number is: 3 and repeated 3 times
The Number is: 13 and repeated 3 times

This post has been edited by modi123_1: 30 October 2017 - 07:24 PM
Reason for edit:: In the future, please use the [code] tag button in the editor..

Is This A Good Question/Topic? 0

## Replies To: Find Dublicate elements in an array with the count

### #2 modi123_1

• Suitor #2

Reputation: 13492
• Posts: 53,892
• Joined: 12-June 08

## Re: Find Dublicate elements in an array with the count

Posted 30 October 2017 - 07:24 PM

Is there a question?

### #3 g00se

• D.I.C Lover

Reputation: 3447
• Posts: 15,614
• Joined: 20-September 08

## Re: Find Dublicate elements in an array with the count

Posted 31 October 2017 - 02:34 AM