Tech-

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



No comments:

Post a Comment