import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class LeastRepeatedNumber {
public static void main(String[] args) {
int[] array1 = {1,1,3,2,3,1,4,2,2};
System.out.println("Least Repeated Number is : "+findLeastRepatedNumber(array1));
}
public static Integer findLeastRepatedNumber(int ...array) {
Integer leastRepatedCount=0;
Map<nteger,Integer> map = new HashMap<>();
Integer leastRepeatedItem=null;
for(int i:array) {
if(map.containsKey(i)) {
int counter = map.get(i)+1;
map.put(i, counter);
}else {
map.put(i,1);
}
}
for(Entry<Integer, Integer> entry : map.entrySet()) {
int v = entry.getValue();
int k = entry.getKey();
if(v>1) {
if(leastRepatedCount==0 || v<leastRepatedCount) {
leastRepatedCount = v;
leastRepeatedItem = k;
}
}
}
return leastRepeatedItem;
}
}
Output:-
Least Repeated Number is : 3
Thursday, August 5, 2021
Least repeated Item in the Integer Array
Subscribe to:
Post Comments (Atom)
How to find middle node of LinkedList without using extra memory in java
To find the middle node of a LinkedList without using extra memory in Java, you can use the "tortoise and hare" algorithm. This al...
-
public class ReverseArrayWithoutExtraMemory { public static void main(String[] args) { int arr[] = {1,2,3,4,5,6}; int swapArray[] =...
-
1) List Interface Implementation public interface List<T extends Comparable<T>> { public Node<T> getMi...
-
function data_change(field) { var check = true; var value = field.value; //get characters //check that all ch...
No comments:
Post a Comment