Related Articles

# Longest subarray with all elements same

• Difficulty Level : Medium
• Last Updated : 14 May, 2021

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:
Explanation:
Longest subarray with equal elements is {2, 2, 2}
Input: arr[] = {1, 1, 2, 2, 2, 3, 3, 3, 3};
Output:
Explanation:
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++

 `// C++ program to find largest``// subarray with all equal elements.` `#include ``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 element 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;``}`

## Java

 `// Java program to find largest``// subarray with all equal elements.``import` `java.util.*;` `class` `GFG{` `// Function to find largest sub``// array with all equal elements.``static` `int` `subarray(``int` `arr[], ``int` `n)``{``    ``int` `ans = ``1``, temp = ``1``;` `    ``// Traverse the array``    ``for``(``int` `i = ``1``; i < n; i++)``    ``{``        ` `       ``// If element is same as``       ``// previous increment temp value``       ``if` `(arr[i] == arr[i - ``1``])``       ``{``           ``++temp;``       ``}``       ``else``       ``{``           ``ans = Math.max(ans, temp);``           ``temp = ``1``;``       ``}``    ``}``    ``ans = Math.max(ans, temp);` `    ``// Return the required answer``    ``return` `ans;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``2``, ``2``, ``1``, ``1``, ``2``,``                  ``2``, ``2``, ``3``, ``3` `};``    ``int` `n = arr.length;` `    ``// Function call``    ``System.out.print(subarray(arr, n));``}``}` `// This code is contributed by AbhiThakur`

## Python3

 `# Python3 program to find largest``# subarray with all equal elements.` `# Function to find largest sub``# array with all equal elements.``def` `subarray(arr, n):``    ` `    ``ans, temp ``=` `1``, ``1` `    ``# Traverse the array``    ``for` `i ``in` `range``(``1``, n):` `        ``# If element is same as previous``        ``# increment temp value``        ``if` `arr[i] ``=``=` `arr[i ``-` `1``]:``            ``temp ``=` `temp ``+` `1``        ``else``:``            ``ans ``=` `max``(ans, temp)``            ``temp ``=` `1``                ` `    ``ans ``=` `max``(ans, temp)` `    ``# Return the required answer``    ``return` `ans` `# Driver code``arr ``=` `[ ``2``, ``2``, ``1``, ``1``, ``2``,``        ``2``, ``2``, ``3``, ``3` `]``n ``=` `len``(arr)` `# Function call``print``(subarray(arr, n))` `# This code is contributed by jrishabh99`

## C#

 `// C# program to find largest``// subarray with all equal elements.``using` `System;``class` `GFG{` `// Function to find largest sub``// array with all equal elements.``static` `int` `subarray(``int``[] arr, ``int` `n)``{``    ``int` `ans = 1, temp = 1;` `    ``// Traverse the array``    ``for``(``int` `i = 1; i < n; i++)``    ``{``        ` `       ``// If element is same as``       ``// previous increment temp value``       ``if` `(arr[i] == arr[i - 1])``       ``{``           ``++temp;``       ``}``       ``else``       ``{``           ``ans = Math.Max(ans, temp);``           ``temp = 1;``       ``}``    ``}``    ``ans = Math.Max(ans, temp);``    ` `    ``// Return the required answer``    ``return` `ans;``}` `// Driver code``public` `static` `void` `Main()``{``    ``int``[] arr = { 2, 2, 1, 1, 2,``                  ``2, 2, 3, 3 };``    ``int` `n = arr.Length;` `    ``// Function call``    ``Console.Write(subarray(arr, n));``}``}` `// This code is contributed by Nidhi_biet`

## Javascript

 ``
Output:

`3`

Time Complexity: O(N)

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.

My Personal Notes arrow_drop_up