# Number of sub-arrays that have at least one duplicate

Given an array *arr* of n elements, the task is to find the number of the sub-arrays of the given array that contain at least one duplicate element.

**Examples:**

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

Output:0

There is no sub-array with duplicate elements.

Input:arr[] = {4, 3, 4, 3}

Output:3

Possible sub-arrays are {4, 3, 4}, {4, 3, 4, 3} and {3, 4, 3}

**Approach:**

- First find the total number of sub-arrays that can be formed from the array and denote this by
*total*then*total = (n*(n+1))/2*. - Now find the sub-arrays that have all the elements distinct (can be found out using window sliding technique) and denote this by
*unique*. - Finally, the number of sub-arrays that have at least one element duplicate are
*(total – unique)*

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to return the count of the ` `// sub-arrays that have at least one duplicate ` `ll count(ll arr[], ll n) ` `{ ` ` ` `ll unique = 0; ` ` ` ` ` `// two pointers ` ` ` `ll i = -1, j = 0; ` ` ` ` ` `// to store frequencies of the numbers ` ` ` `unordered_map<ll, ll> freq; ` ` ` `for` `(j = 0; j < n; j++) { ` ` ` `freq[arr[j]]++; ` ` ` ` ` `// number is not distinct ` ` ` `if` `(freq[arr[j]] >= 2) { ` ` ` `i++; ` ` ` `while` `(arr[i] != arr[j]) { ` ` ` `freq[arr[i]]--; ` ` ` `i++; ` ` ` `} ` ` ` `freq[arr[i]]--; ` ` ` `unique = unique + (j - i); ` ` ` `} ` ` ` `else` ` ` `unique = unique + (j - i); ` ` ` `} ` ` ` ` ` `ll total = n * (n + 1) / 2; ` ` ` ` ` `return` `total - unique; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll arr[] = { 4, 3, 4, 3 }; ` ` ` `ll n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << count(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

from collections import defaultdict

# Function to return the count of the

# sub-arrays that have at least one duplicate

def count(arr, n):

unique = 0

# two pointers

i, j = -1, 0

# to store frequencies of the numbers

freq = defaultdict(lambda:0)

for j in range(0, n):

freq[arr[j]] += 1

# number is not distinct

if freq[arr[j]] >= 2:

i += 1

while arr[i] != arr[j]:

freq[arr[i]] -= 1

i += 1

freq[arr[i]] -= 1

unique = unique + (j – i)

else:

unique = unique + (j – i)

total = (n * (n + 1)) // 2

return total – unique

# Driver Code

if __name__ == “__main__”:

arr = [4, 3, 4, 3]

n = len(arr)

print(count(arr, n))

# This code is contributed

# by Rituraj Jain

**Output:**

3

## Recommended Posts:

- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Number of subarrays with odd sum
- Number of subarrays having sum less than K
- Find number of subarrays with even sum
- Number of subarrays have bitwise OR >= K
- Number of subarrays with given product
- Number of subarrays with m odd numbers
- Number of subarrays having sum exactly equal to k
- Number of subarrays having sum in a given range
- Maximize the number of subarrays with XOR as zero
- Count the number of subarrays having a given XOR
- Number of subarrays having product less than K
- Count subarrays with equal number of 1's and 0's
- Number of subarrays such that XOR of one half is equal to the other
- Number of subarrays for which product and sum are equal

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.