Try this one if you want to print no. of times of Duplicate element came public class Repeating { public static void main(String[] args) { int arr[] = { 10, 20, 30, 10, 10, 20, 40, 10 };
Integer temp; HashMap map = new HashMap(); for (int i = 0; i < arr.length; i++) { temp = (Integer) arr[i]; if (map.get(temp) != null) { map.put(temp, map.get(temp) + 1); } else { map.put(temp, 1); } } for (Entry entry : map.entrySet()) { { if (entry.getValue() > 1) { System.out.print(entry.getKey()); System.out.print("-:"); System.out.print(entry.getValue()); System.out.print("");
It's useful for me.... Thank you sir
Sandeep please cover all the scenarios .then only it will helped
just say "!set.add(e)", the add method on set return false if the element already exists, this will simplify code further.
Try this one if you want to print no. of times of Duplicate element came
public class Repeating {
public static void main(String[] args) {
int arr[] = { 10, 20, 30, 10, 10, 20, 40, 10 };
Integer temp;
HashMap map = new HashMap();
for (int i = 0; i < arr.length; i++) {
temp = (Integer) arr[i];
if (map.get(temp) != null) {
map.put(temp, map.get(temp) + 1);
} else {
map.put(temp, 1);
}
}
for (Entry entry : map.entrySet()) {
{
if (entry.getValue() > 1) {
System.out.print(entry.getKey());
System.out.print("-:");
System.out.print(entry.getValue());
System.out.print("");
}
}
}
}
}
If we have more than one duplicate, it will print double times.
Screen was not clear
Probably issue with your data