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, 8Output:2Explanation: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, 5Output :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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Find element using minimum segments in Seven Segment Display
- Divide an array into k segments to maximize maximum of segment minimums
- Equally divide into two sets such that one set has maximum distinct elements
- Form an array of distinct elements with each element as sum of an element from each array
- Find middle point segment from given segment lengths
- Minimum number of distinct powers of 2 required to express a given binary number
- Find minimum changes required in an array for it to contain k distinct elements
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Number of segments where all elements are greater than X
- Partition an array such into maximum increasing segments
- Divide N segments into two non-empty groups such that given condition is satisfied
- Divide the array in K segments such that the sum of minimums is maximized
- Minimum deletions required to make frequency of each letter unique
- Minimum number of towers required such that every house is in the range of at least one tower
- Minimum deletions required such that any number X will occur exactly X times
- Count distinct elements after adding each element of First Array with Second Array
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.