Number of sub-arrays that have at least one duplicate
Last Updated :
09 Sep, 2022
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++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll count(ll arr[], ll n)
{
ll unique = 0;
ll i = -1, j = 0;
unordered_map<ll, ll> freq;
for (j = 0; j < n; j++) {
freq[arr[j]]++;
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;
}
int main()
{
ll arr[] = { 4, 3, 4, 3 };
ll n = sizeof (arr) / sizeof (arr[0]);
cout << count(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Integer count(Integer arr[], Integer n)
{
Integer unique = 0 ;
Integer i = - 1 , j = 0 ;
Map<Integer, Integer> freq = new HashMap<>();
for (j = 0 ; j < n; j++)
{
if (freq.containsKey(arr[j]))
{
freq.put(arr[j], freq.get(arr[j]) + 1 );
}
else
{
freq.put(arr[j], 1 );
}
if (freq.get(arr[j]) >= 2 )
{
i++;
while (arr[i] != arr[j])
{
freq.put(arr[i], freq.get(arr[i]) - 1 );
i++;
}
freq.put(arr[i], freq.get(arr[i]) - 1 );
unique = unique + (j - i);
}
else
unique = unique + (j - i);
}
Integer total = n * (n + 1 ) / 2 ;
return total - unique;
}
public static void main(String[] args)
{
Integer arr[] = { 4 , 3 , 4 , 3 };
Integer n = arr.length;
System.out.println(count(arr, n));
}
}
|
Python3
from collections import defaultdict
def count(arr, n):
unique = 0
i, j = - 1 , 0
freq = defaultdict( lambda : 0 )
for j in range ( 0 , n):
freq[arr[j]] + = 1
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
if __name__ = = "__main__" :
arr = [ 4 , 3 , 4 , 3 ]
n = len (arr)
print (count(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int count( int []arr, int n)
{
int unique = 0;
int i = -1, j = 0;
Dictionary< int ,
int > freq = new Dictionary< int ,
int >();
for (j = 0; j < n; j++)
{
if (freq.ContainsKey(arr[j]))
{
freq[arr[j]] = freq[arr[j]] + 1;
}
else
{
freq.Add(arr[j], 1);
}
if (freq[arr[j]] >= 2)
{
i++;
while (arr[i] != arr[j])
{
freq[arr[i]] = freq[arr[i]] - 1;
i++;
}
freq[arr[i]] = freq[arr[i]] - 1;
unique = unique + (j - i);
}
else
unique = unique + (j - i);
}
int total = n * (n + 1) / 2;
return total - unique;
}
public static void Main(String[] args)
{
int []arr = { 4, 3, 4, 3 };
int n = arr.Length;
Console.WriteLine(count(arr, n));
}
}
|
Javascript
<script>
function count(arr, n)
{
let unique = 0;
let i = -1, j = 0;
let freq = new Map();
for (j = 0; j < n; j++) {
if (freq.has(arr[j])){
freq.set(arr[j], freq.get(arr[j]) + 1)
} else {
freq.set(arr[j], 1)
}
if (freq.get(arr[j]) >= 2) {
i++;
while (arr[i] != arr[j]) {
freq.set(arr[i], freq.get(arr[i]) - 1)
i++;
}
freq.set(arr[i], freq.get(arr[i]) - 1)
unique = unique + (j - i);
}
else
unique = unique + (j - i);
}
let total = n *(n + 1) / 2;
return total - unique;
}
let arr = [ 4, 3, 4, 3 ];
let n = arr.length;
document.write(count(arr, n) + "<br>" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...