# Minimum number of segments required such that each segment has distinct elements

• Last Updated : 06 Sep, 2022

Given an array of integers, the task is to find the minimum number of segments that the array elements can be divided into such that all the segments contain distinct elements.

Examples:

```Input: n=6 ; Array: 1, 7, 4, 3, 3, 8
Output: 2
Explanation:
Optimal way to create segments here is {1, 7, 4, 3} {3, 8}
Clearly, the answer is the maximum frequency of any element within the array i.e. '2'.
as '3' is the element which appears the most in the array (twice).

Input : n=5 ; Array: 2, 2, 3, 3, 3, 5
Output : 3```

Approach:

• The optimal approach is to put all the distinct elements in a single segment.
• And then, put all other elements that have several occurrences one by one in new segments such that no segment contains repetitions of elements.
• So, the answer is the maximum frequency of any element within the given array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function that counts the``// minimum segments required``void` `CountSegments(``int` `N, ``int` `a[])``{``    ``// all values are '0' initially``    ``int` `frequency[10001] = { 0 };` `    ``// count of segments``    ``int` `c = 0;` `    ``// store frequency of every element``    ``for` `(``int` `i = 0; i < N; i++) {``        ``frequency[a[i]]++;``    ``}` `    ``// find maximum frequency``    ``for` `(``int` `i = 0; i <= 10000; i++)``        ``c = max(c, frequency[i]);` `    ``cout << c << ``"\n"``;``}` `// Driver code``int` `main()``{``    ``int` `N = 6;``    ``int` `a[] = { 1, 3, 4, 3, 2, 3 };` `    ``CountSegments(N, a);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach` `import` `java.io.*;` `class` `GFG {` `// Function that counts the``// minimum segments required``static` `void` `CountSegments(``int` `N, ``int` `a[])``{``    ``// all values are '0' initially``    ``int` `frequency[] =  ``new` `int``[``10001``];` `    ``// count of segments``    ``int` `c = ``0``;` `    ``// store frequency of every element``    ``for` `(``int` `i = ``0``; i < N; i++) {``        ``frequency[a[i]]++;``    ``}` `    ``// find maximum frequency``    ``for` `(``int` `i = ``0``; i <= ``10000``; i++)``        ``c = Math.max(c, frequency[i]);` `    ``System.out.println( c);``}` `       ``// Driver code``    ``public` `static` `void` `main (String[] args) {``        ``int` `N = ``6``;``    ``int` `[]a = { ``1``, ``3``, ``4``, ``3``, ``2``, ``3` `};` `    ``CountSegments(N, a);``    ``}``}` `// This Code is contributed by inder_verma..`

## Python 3

 `# Python3 implementation of the approach` `# Function that counts the``# minimum segments required``def` `CountSegments(N, a):``    ` `    ``# all values are '0' initially``    ``frequency ``=` `[``0``] ``*` `10001` `    ``# count of segments``    ``c ``=` `0` `    ``# store frequency of every element``    ``for` `i ``in` `range``(N) :``        ``frequency[a[i]] ``+``=` `1` `    ``# find maximum frequency``    ``for` `i ``in` `range``(``10001``):``        ``c ``=` `max``(c, frequency[i])` `    ``print``(c)` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``N ``=` `6``    ``a ``=` `[ ``1``, ``3``, ``4``, ``3``, ``2``, ``3` `]``    ``CountSegments(N, a)` `# This code is contributed``# by ChitraNayal`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `// Function that counts the``// minimum segments required``static` `void` `CountSegments(``int` `N, ``int` `[]a)``{``    ``// all values are '0' initially``    ``int` `[]frequency = ``new` `int``[10001];` `    ``// count of segments``    ``int` `c = 0;` `    ``// store frequency of every element``    ``for` `(``int` `i = 0; i < N; i++)``    ``{``        ``frequency[a[i]]++;``    ``}` `    ``// find maximum frequency``    ``for` `(``int` `i = 0; i <= 10000; i++)``        ``c = Math.Max(c, frequency[i]);` `    ``Console.WriteLine( c);``}` `// Driver code``public` `static` `void` `Main ()``{``    ``int` `N = 6;``    ``int` `[]a = { 1, 3, 4, 3, 2, 3 };` `    ``CountSegments(N, a);``}``}` `// This code is contributed``// by inder_verma`

## Javascript

 ``

Output

```3
```

My Personal Notes arrow_drop_up