# Find first repeating element in array

Here given code implementation process.

```
// C++ Program
// Find first repeating element in array
#include <iostream>
#include <set>
using namespace std;
// Find the first element of array which are repeated from left to right
void firstRepeating(int arr[], int size)
{
// Define a empty set
set < int > data;
bool status = false;
int result = 0;
// Execute loop through by array size
for (int i = size-1; i >= 0; --i)
{
if(data.find(arr[i]) == data.end())
{
data.insert(arr[i]);
}
else
{
// When get a new result
result = arr[i];
status = true;
}
}
if (status == false)
{
cout << "None" << endl;
}
else
{
cout << result <<endl;
}
}
int main()
{
// Define array of integer element
int arr[] =
{
3 , 9 , 7 , 6, 7 , 2 , 9 , 1 , 7 , 6
};
// Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
firstRepeating(arr, size);
return 0;
}
```

#### Output

`9`

```
/*
Java program
Find first repeating element in array
*/
import java.util.HashSet;
public class Counting
{
// Find the first element of array which are repeated from left to right
public void firstRepeating(int[] arr)
{
// This is used to collect result
int result = 0;
// Result indicator
boolean status = false;
// Create a empty hashSet
HashSet <Integer> data = new HashSet<>();
// iterate through by array size
for (int i = arr.length - 1; i > 0; --i)
{
if (data.contains(arr[i]))
{
// When elements are already exists
status = true;
result = arr[i];
}
else
{
// When inserting new element
data.add(arr[i]);
}
}
if (status == true)
{
// When result exists
System.out.print(result);
}
else
{
// When result not exists
System.out.print(" None \n");
}
}
public static void main(String[] args)
{
Counting task = new Counting();
// Get the size of array
int[] arr = {
3 , 9 , 7 , 6 , 7 , 2 , 9 , 1 , 7 , 6
};
task.firstRepeating(arr);
}
}
```

#### Output

`9`

```
// Include namespace system
using System;
using System.Collections.Generic;
public class Counting
{
// Find the first element of array which are repeated from left to right
public void firstRepeating(int[] arr)
{
// This is used to collect result
int result = 0;
// Result indicator
Boolean status = false;
// Create a empty hashSet
HashSet <int> data = new HashSet <int> ();
// iterate through by array size
for (int i = arr.Length - 1; i > 0; --i)
{
if (data.Contains(arr[i]))
{
// When elements are already exists
status = true;
result = arr[i];
}
else
{
// When inserting new element
data.Add(arr[i]);
}
}
if (status == true)
{
// When result exists
Console.Write(result);
}
else
{
// When result not exists
Console.Write(" None \n");
}
}
public static void Main(String[] args)
{
Counting task = new Counting();
// Get the size of array
int[] arr = {
3 , 9 , 7 , 6 , 7 , 2 , 9 , 1 , 7 , 6
};
task.firstRepeating(arr);
}
}
```

#### Output

`9`

```
<?php
// Php Program
// Find first repeating element in array
class Counting
{
// Find the first element of array which are repeated from left to right
public function firstRepeating( & $arr)
{
// This is used to collect result
$result = 0;
// Result indicator
$status = false;
// Create a empty array
$data = [];
// iterate through by array size
for ($i = count($arr) - 1; $i > 0; --$i)
{
if (array_key_exists($arr[$i],$data))
{
// When elements are already exists
$status = true;
$result = $arr[$i];
}
else
{
// When inserting new element
$data[$arr[$i]]=1;
}
}
if ($status == true)
{
// When result exists
echo $result;
}
else
{
// When result not exists
echo " None \n";
}
}
}
function main()
{
$task = new Counting();
// Get the size of array
$arr = array(3, 9, 7, 6, 7, 2, 9, 1, 7, 6);
$task->firstRepeating($arr);
}
main();
```

#### Output

`9`

```
class Counting
{
// Find the first element of array which are repeated from left to right
firstRepeating(arr)
{
// This is used to collect result
var result = 0;
// Result indicator
var status = false;
// Create a empty Set
var data = new Set();;
// iterate through by array size
for (var i = arr.length - 1; i > 0; --i)
{
if (data.has(arr[i]))
{
// When elements are already exists
status = true;
result = arr[i];
}
else
{
// When inserting new element
data.add(arr[i]);
}
}
if (status == true)
{
// When result exists
console.log(result);
}
else
{
// When result not exists
console.log(" None \n");
}
}
}
function main()
{
var task = new Counting();
// Get the size of array
var arr = [3, 9, 7, 6, 7, 2, 9, 1, 7, 6];
task.firstRepeating(arr);
}
main();
```

#### Output

`9`

```
# Python 3 program
# Find first repeating element in array
class Counting :
# Find the first element of list which are repeated from left to right
def firstRepeating(self, arr) :
# This is used to collect result
result = 0
# Result indicator
status = False
# Create a empty Set
data = set()
i = len(arr) - 1
# iterate through by list size
while (i > 0) :
if (arr[i] in data) :
# When elements are already exists
status = True
result = arr[i]
else :
# When inserting new element
data.add(arr[i])
i -= 1
if (status == True) :
# When result exists
print(result, end = "")
else :
# When result not exists
print(" None ")
def main() :
task = Counting()
# Get the size of list
arr = [3, 9, 7, 6, 7, 2, 9, 1, 7, 6]
task.firstRepeating(arr)
if __name__ == "__main__": main()
```

#### Output

`9`

```
# Ruby program
# Find first repeating element in array
class Counting
# Find the first element of array which are repeated from left to right
def firstRepeating(arr)
# This is used to collect result
result = 0
# Result indicator
status = false
# Create a empty array
data = []
i = arr.length - 1
# iterate through by array size
while (i > 0)
if (data.include? (arr[i]))
# When elements are already exists
status = true
result = arr[i]
else
# When inserting new element
data.push(arr[i])
end
i -= 1
end
if (status == true)
# When result exists
print(result)
else
# When result not exists
print(" None \n")
end
end
end
def main()
task = Counting.new()
# Get the size of array
arr = [3, 9, 7, 6, 7, 2, 9, 1, 7, 6]
task.firstRepeating(arr)
end
main()
```

#### Output

`9`

```
import scala.collection.mutable._ ;
/*
Scala program
Find first repeating element in array
*/
class Counting
{
// Find the first element of array which are repeated from left to right
def firstRepeating(arr: Array[Int]): Unit = {
// This is used to collect result
var result: Int = 0;
// Result indicator
var status: Boolean = false;
// Create a empty Set
var data : Set[Int] = Set();
var i: Int = arr.length - 1;
// iterate through by array size
while (i > 0)
{
if (data.contains(arr(i))==true)
{
// When elements are already exists
status = true;
result = arr(i);
}
else
{
// When inserting new element
data.add(arr(i));
}
i -= 1;
}
if (status == true)
{
// When result exists
print(result);
}
else
{
// When result not exists
print(" None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
// Get the size of array
var arr: Array[Int] = Array(3, 9, 7, 6, 7, 2, 9, 1, 7, 6);
task.firstRepeating(arr);
}
}
```

#### Output

`9`

```
/*
Swift 4 program
Find first repeating element in array
*/
class Counting
{
// Find the first element of array which are repeated from left to right
func firstRepeating(_ arr: [Int])
{
// This is used to collect result
var result: Int = 0;
// Result indicator
var status: Bool = false;
// Create a empty Set
var data = Set<Int>()
var i: Int = arr.count - 1;
// iterate through by array size
while (i > 0)
{
if (data.contains(arr[i]))
{
// When elements are already exists
status = true;
result = arr[i];
}
else
{
// When inserting new element
data.insert(arr[i]);
}
i -= 1;
}
if (status == true)
{
// When result exists
print(result, terminator: "");
}
else
{
// When result not exists
print(" None ");
}
}
}
func main()
{
let task: Counting = Counting();
// Get the size of array
let arr: [Int] = [3, 9, 7, 6, 7, 2, 9, 1, 7, 6];
task.firstRepeating(arr);
}
main();
```

#### Output

`9`

```
/*
Kotlin program
Find first repeating element in array
*/
class Counting
{
// Find the first element of array which are repeated from left to right
fun firstRepeating(arr: Array < Int > ): Unit
{
// This is used to collect result
var result: Int = 0;
// Result indicator
var status: Boolean = false;
// Create a empty Set
var data: MutableSet < Int > = mutableSetOf < Int > ();
var i: Int = arr.count() - 1;
// iterate through by array size
while (i > 0)
{
if (data.contains(arr[i]))
{
// When elements are already exists
status = true;
result = arr[i];
}
else
{
data.add(arr[i]);
}
i -= 1;
}
if (status == true)
{
// When result exists
print(result);
}
else
{
// When result not exists
print(" None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Counting = Counting();
// Get the size of array
var arr: Array < Int > = arrayOf(3, 9, 7, 6, 7, 2, 9, 1, 7, 6);
task.firstRepeating(arr);
}
```

#### Output

`9`

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

## New Comment