# Minimum delete operations to make all elements of array same in Java

Hello friends, today we are going to see the Java implementation of minimum delete operations to make all elements of the array the same. Firstly we will see an algorithm to demonstrate the approach to solving this problem.

### Algorithm

See the algorithm below:

- Start
- All the necessary elements are taken from the user.
- Declare an empty array for finding the frequency count.
- Count all the frequencies
- Find the maximum among all the frequencies.
- After, following all the steps then subtract from the n elements.
- End

### Java Code

Firstly, take all the inputs:

import java.util.*; public class MinDelOp { public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println("Enter n number of element's"); int n = in.nextInt(); int a[]= new int[n]; int f[]= new int[n]; //declaring empty array for frequency count System.out.println("Enter all the element's"); for(int i=0;i<n;i++){ a[i]=in.nextInt(); // taking input for an array }

Secondly, find the frequency of an array.

for(int i=0;i<n;i++){ f[a[i]]++; //counting frequency }

Then find the maximum from the frequency:

int max=Integer.MIN_VALUE; //intitalizing minimum value for(int i=0;i<n;i++){ max=Math.max(max,f[i]); }

Finally, print the minimum number of delete operations to make an array the same.

System.out.println("Number of minimum delete operations : "+(n-max)); } }

Output

Enter n number of element's 6 Enter all the element's 1 4 4 2 4 3 Number of minimum delete operations : 3

## Leave a Reply