Thursday, January 11, 2018

function to print the repeated values in an array with it's count

public static void printRepeating(int arr[], int size)
{
int i,j;
System.out.println("The repeating elements are : ");
HashMap<Integer, Integer> hmap = new HashMap<>();

for (i = 0; i < size-1; i++)
{
int c=1;
for (j = i+1; j < size; j++)
{

if(arr[i]==arr[j]){
c++;
}
}
if(c>1&& !hmap.containsValue(arr[i])){

hmap.put(c,arr[i]);

}
}

for (int k:hmap.keySet()){
System.out.println(hmap.get(k)+" : count "+k);
}
}


No comments:

Post a Comment