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

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;` `}` |

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

`<script>` `// js implementation of the approach` `// Function that counts the` `// minimum segments required` `function` `CountSegments(N,a)` `{` ` ` `// all values are '0' initially` ` ` `var` `frequency = ` `new` `Array(10001).fill(0);` ` ` `// count of segments` ` ` `let c = 0;` ` ` `// store frequency of every element` ` ` `for` `(` `var` `i = 0; i < N; i++)` ` ` `{` ` ` `frequency[a[i]]++;` ` ` `}` ` ` `// find maximum frequency` ` ` `for` `(` `var` `i = 0; i <= 10000; i++)` ` ` `c = Math.max(c, frequency[i]);` ` ` `document.write(c);` `}` `// Driver code` `var` `N = 6;` `var` `a = [1, 3, 4, 3, 2, 3 ];` `CountSegments(N, a);` `</script>` |

**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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.