Given an array **arr[]** of size **N**, the task is to find the largest subarray which consists of all equal elements.

**Examples:**

Input:arr[] = {1, 1, 2, 2, 2, 3, 3};Output:3Explanation:

Longest subarray with equal elements is {2, 2, 2}

Input:arr[] = {1, 1, 2, 2, 2, 3, 3, 3, 3};Output:4Explanation:

Longest subarray with equal elements is {3, 3, 3, 3}

**Approach:** The idea is to traverse the array and check that the current element is equal to the previous element or not. If yes then increment the length of the longest subarray by 1. Otherwise, the current longest subarray is equal to 1. Also, update the longest subarray with equal elements at each step of the iteration.

Below is the implementation of the above approach:

`// C++ program to find largest ` `// subarray with all equal elements. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to find largest sub ` `// array with all equal elements. ` `int` `subarray(` `int` `arr[], ` `int` `n) `
`{ ` ` ` ` ` `int` `ans = 1, temp = 1; `
` ` ` ` `// Traverse the array `
` ` `for` `(` `int` `i = 1; i < n; i++) { `
` ` ` ` `// If elemet is same as `
` ` `// previous increment temp value `
` ` `if` `(arr[i] == arr[i - 1]) { `
` ` `++temp; `
` ` `} `
` ` `else` `{ `
` ` `ans = max(ans, temp); `
` ` `temp = 1; `
` ` `} `
` ` `} `
` ` `ans = max(ans, temp); `
` ` ` ` `// Return the required answer `
` ` `return` `ans; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `arr[] = { 2, 2, 1, 1, `
` ` `2, 2, 2, 3, 3 }; `
` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); `
` ` ` ` `// Function call `
` ` `cout << subarray(arr, n); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity**: O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Longest subarray not having more than K distinct elements
- Longest Subarray having sum of elements atmost 'k'
- Longest subarray in which all elements are greater than K
- Length of the longest Subarray with only Even Elements
- Longest subarray with elements having equal modulo K
- Length of Longest Subarray with same elements in atmost K increments
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Length of longest Subarray with equal number of odd and even elements
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Longest subarray whose elements can be made equal by maximum K increments
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Maximum length of subarray such that all elements are equal in the subarray
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Longest subarray having sum K | Set 2
- Longest subarray such that difference of max and min is at-most K
- Longest subarray having maximum sum
- Longest Mountain Subarray
- Longest increasing subarray
- Longest subarray with sum divisible by 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.