2 Replies - 248 Views - Last Post: 31 October 2017 - 02:34 AM Rate Topic: -----

#1 nihal0808  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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");
 }

 }
 }
 }
}

:code:

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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13492
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Find Dublicate elements in an array with the count

Posted 31 October 2017 - 02:34 AM

It won't help you (nor will it help people here to help you) that your code is badly indented/formatted. See
http://technojeeves....-code-formatter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1