Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.
Example
Arr = [1,2,2,3]
Delete the 2 elements 1 and 3 leaving arr=[2,2]. If both twos plus either the 1 or 3 are deleted, it takes 3 deletions to leave either [3] or [1]. The minimum number of deletions is 2.
Function Description
Complete the equalizeArray function in the editor below.
equalizeArray has the following parameter(s):
- int arr[n]: an array of integers
Returns
- int: the minimum number of deletions required
Input Format
The first line contains an integer , the number of elements in arr.
The next line contains space-separated integers arr[i].
Constraints
- 1<=n<=100
- 1<=arr[i]<=100
Sample Input
STDIN Function ----- -------- 5 arr[] size n = 5 3 3 2 1 3 arr = [3, 3, 2, 1, 3]
Sample Output
2
Equalize the Array HackerRank Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int i,n,l; scanf("%d",&n); int arr[n]; for(i=0;i<n;i++){scanf("%d",&arr[i]);} int brr[101]={0}; for(i=0;i<n;i++){brr[arr[i]]++;} l=0; for(i=0;i<101;i++){ if(brr[i]>l){l=brr[i];} } printf("%d",n-l); return 0; }
Equalize the Array HackerRank Solution in C++
#include <bits/stdc++.h> using namespace std; #define DEBUG(x) cerr << #x << " = " << x << endl #define INPUT freopen("Data.inp", "r", stdin) #define OUTPUT freopen("Data.out", "w", stdout) typedef long long LL; typedef pair<int, int> II; typedef vector<int> VI; const int N = (int) 1e2 + 10; int n, a[N], c[N]; int main() { #ifdef LOCAL INPUT; OUTPUT; #endif scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) c[a[i]]++; printf("%d", n - *max_element(c + 1, c + 101)); return 0; }
Equalize the Array HackerRank Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] counts = new int[101]; for (int i = 0; i < n; i++) { counts[sc.nextInt()]++; } Arrays.sort(counts); System.out.println(n-counts[100]); } }
Equalize the Array HackerRank Solution in Python
from collections import Counter n = input() A = map(int,raw_input().strip().split()) cts = Counter(A) print n-max(cts.values())
Equalize the Array HackerRank Solution in C#
using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { int t = Convert.ToInt32(Console.ReadLine()); string[] a_temp = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(a_temp,Int32.Parse); int[] counts = new int[101]; for (int i = 0; i < t; ++i) { counts[a[i]]++; } int maxValue = 0; for (int i = 0; i < 101; ++i) { maxValue = Math.Max(maxValue, counts[i]); } Console.Out.WriteLine(t - maxValue); } }
Attempt Equalize the Array HackerRank Challenge
Link – https://www.hackerrank.com/challenges/equality-in-a-array/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.