Find the repeating element in an Array of size N consisting of first M natural numbers
Last Updated :
24 Apr, 2023
Given an array arr[] of size N, which contains a permutation of numbers from 1 to M, as well as an element that is repeated(one or more times), the task is to find the repeating element.
Examples:
Input: arr[]={2, 6, 4, 3, 1, 5, 2}, N=7
Output:
2
Explanation: In arr[], all elements from 0 to 6 occurs once, except 2 which is repeated once.
Input: arr[]={2, 1, 3, 1, 1, 1}, N=6
Output:
1
Naive Approach: The naive approach would be to sort the array and check for adjacent elements that are equal.
C++
#include <bits/stdc++.h>
using namespace std;
int findRepeatingElement( int arr[], int n, int m) {
sort(arr, arr + n);
for ( int i = 0; i < n-1; i++) {
if (arr[i] == arr[i + 1]) {
return arr[i];
}
}
return -1;
}
int main() {
int arr[] = { 2, 6, 4, 3, 1, 5, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int m = 4;
int repeating_element = findRepeatingElement(arr, n, m);
cout<< repeating_element << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static int findRepeatingElement( int [] arr, int n, int m) {
Arrays.sort(arr);
for ( int i = 0 ; i < n - 1 ; i++) {
if (arr[i] == arr[i + 1 ]) {
return arr[i];
}
}
return - 1 ;
}
public static void main(String[] args) {
int [] arr = { 2 , 1 , 3 , 1 , 1 , 1 };
int n = arr.length;
int m = 4 ;
int repeating_element = findRepeatingElement(arr, n, m);
System.out.println(repeating_element);
}
}
|
Python3
def find_repeating_element(arr, n, m):
arr.sort()
for i in range (n - 1 ):
if arr[i] = = arr[i + 1 ]:
return arr[i]
return - 1
arr = [ 2 , 6 , 4 , 3 , 1 , 5 , 2 ]
n = len (arr)
m = 4
repeating_element = find_repeating_element(arr, n, m)
print (repeating_element)
|
C#
using System;
class Program
{
static int findRepeatingElement( int [] arr, int n, int m)
{
Array.Sort(arr);
for ( int i = 0; i < n-1; i++) {
if (arr[i] == arr[i + 1]) {
return arr[i];
}
}
return -1;
}
static void Main( string [] args)
{
int [] arr = { 2, 6, 4, 3, 1, 5, 2 };
int n = arr.Length;
int m = 4;
int repeating_element = findRepeatingElement(arr, n, m);
Console.WriteLine(repeating_element);
}
}
|
Javascript
function findRepeatingElement(arr, n, m) {
arr.sort();
for (let i = 0; i < n-1; i++) {
if (arr[i] == arr[i + 1]) {
return arr[i];
}
}
return -1;
}
let arr = [2, 6, 4, 3, 1, 5, 2];
let n = arr.length;
let m = 4;
let repeating_element = findRepeatingElement(arr, n, m);
console.log(repeating_element);
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Approach: Follow the steps below to solve the problem:
- Initialize two variables M and sum to store the maximum element and the sum of the array respectively.
- Traverse array arr and do the following:
- Add the current element to sum
- Compare the current element to M to calculate the maximum element.
- Store the sum of permutation from 1 to M in a variable say, sum1, using the formula:
Sum of elements from 1 to X= X*(X+1)/2
- Calculate the answer as the difference between sum and sum1 divided by the number of extra characters i.e. (sum-sum1)/(N-M).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int repeatingElement( int arr[], int N)
{
int M = 0, sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
M = max(M, arr[i]);
}
int sum1 = M * (M + 1) / 2;
int ans = (sum - sum1) / (N - M);
return ans;
}
int main()
{
int arr[] = { 2, 6, 4, 3, 1, 5, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << repeatingElement(arr, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int repeatingElement( int arr[], int N)
{
int M = 0 , sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += arr[i];
M = Math.max(M, arr[i]);
}
int sum1 = M * (M + 1 ) / 2 ;
int ans = (sum - sum1) / (N - M);
return ans;
}
public static void main (String[] args)
{
int arr[] = { 2 , 6 , 4 , 3 , 1 , 5 , 2 };
int N = arr.length;
System.out.println(repeatingElement(arr, N));
}
}
|
Python3
def repeatingElement(arr, N):
M = 0
sum = 0
for i in range (N):
sum + = arr[i]
M = max (M, arr[i])
sum1 = M * (M + 1 ) / / 2
ans = ( sum - sum1) / / (N - M)
return ans
if __name__ = = '__main__' :
arr = [ 2 , 6 , 4 , 3 , 1 , 5 , 2 ]
N = len (arr)
print (repeatingElement(arr, N))
|
C#
using System;
public class GFG
{
public static int repeatingElement( int [] arr, int N)
{
int M = 0, sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
M = Math.Max(M, arr[i]);
}
int sum1 = M * (M + 1) / 2;
int ans = (sum - sum1) / (N - M);
return ans;
}
public static void Main()
{
int [] arr = { 2, 6, 4, 3, 1, 5, 2 };
int N = 7;
Console.WriteLine(repeatingElement(arr, N));
}
}
|
Javascript
function repeatingElement(arr, N)
{
let M = 0, sum = 0;
for (let i = 0; i < N; i++) {
sum += arr[i];
M = Math.max(M, arr[i]);
}
let sum1 = parseInt(M * (M + 1) / 2);
let ans = parseInt((sum - sum1) / (N - M));
return ans;
}
let arr = [2, 6, 4, 3, 1, 5, 2];
let N = arr.length;
document.write(repeatingElement(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...