# Find the element that appears once in array

Given of collection of integer elements. That is contains one single element and remaining elements are appears exactly twice. Our goal is to find that single element which is unique of this collection.

Here given code implementation process.

``````// C Program
// Find the element that appears once in array
#include <stdio.h>

//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
}
// Finds the single appearing element which is exist in repeating element
void single_element(int arr[], int size)
{
int result = arr;
for (int i = 1; i < size; ++i)
{
// Perform xor operation
result = result ^ arr[i];
}
display(arr, size);
// Display calculated result
printf("\n  Single element %d\n\n", result);
}
int main()
{
// Arrays which is containing single and double(two times) repeating elements
int arr1[] = {
8 , 7 , 9 , 5 , 8 , 9 , 7
};
int arr2[] = {
1 , 1 , 9 , 5 , 6 , 6 , 5,  4,  4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1);
single_element(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2);
single_element(arr2, size);
return 0;
}``````

#### Output

``````  8  7  9  5  8  9  7
Single element 5

1  1  9  5  6  6  5
Single element 9
``````
``````/*
Java Program
Find the element that appears once in array
*/
public class SingleElement
{

//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("   " + arr[i] );
}
}
// Finds the single appearing element which is exist in repeating element
public void single_element(int[] arr, int size)
{
int result = arr;
for (int i = 1; i < size; ++i)
{
// Perform xor operation
result = result ^ arr[i];
}
display(arr, size);
// Display calculated result
System.out.print("\n   Single element " + result + "\n\n");
}
public static void main(String[] args)
{
SingleElement find = new SingleElement();
// Arrays which is containing single and double(two times) repeating elements
int[] arr1 =  {
8 , 7 , 9 , 5 , 8 , 9 , 7
};
int[] arr2 = {
1 , 1 , 9 , 5 , 6 , 6 , 5 , 4, 4
};
// Get the size
int size = arr1.length;
find.single_element(arr1, size);
// Get the size
size = arr2.length;
find.single_element(arr2, size);

}
}``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9
``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find the element that appears once in array
*/
class SingleElement
{
public:
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "   " << arr[i];
}
}
// Finds the single appearing element which is exist in repeating element
void single_element(int arr[], int size)
{
int result = arr;
for (int i = 1; i < size; ++i)
{
// Perform xor operation
result = result ^ arr[i];
}
this->display(arr, size);
// Display calculated result
cout << "\n   Single element " << result << "\n\n";
}
};
int main()
{
SingleElement find = SingleElement();
// Arrays which is containing single and double(two times) repeating elements
int arr1[] = {
8 , 7 , 9 , 5 , 8 , 9 , 7
};
int arr2[] = {
1 , 1 , 9 , 5 , 6 , 6 , 5 , 4 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1);
find.single_element(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2);
find.single_element(arr2, size);
return 0;
}``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9
``````
``````// Include namespace system
using System;
/*
C# Program
Find the element that appears once in array
*/
public class SingleElement
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("   " + arr[i]);
}
}
// Finds the single appearing element which is exist in repeating element
public void single_element(int[] arr, int size)
{
int result = arr;
for (int i = 1; i < size; ++i)
{
// Perform xor operation
result = result ^ arr[i];
}
display(arr, size);
// Display calculated result
Console.Write("\n   Single element " + result + "\n\n");
}
public static void Main(String[] args)
{
SingleElement find = new SingleElement();
// Arrays which is containing single and double(two times) repeating elements
int[] arr1 = {
8 , 7 , 9 , 5 , 8 , 9 , 7
};
int[] arr2 = {
1 , 1 , 9 , 5 , 6 , 6 , 5 , 4 , 4
};
// Get the size
int size = arr1.Length;
find.single_element(arr1, size);
// Get the size
size = arr2.Length;
find.single_element(arr2, size);
}
}``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9
``````
``````<?php
/*
Php Program
Find the element that appears once in array
*/
class SingleElement
{
//Function which is display array elements
public	function display( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "   ". \$arr[\$i];
}
}
// Finds the single appearing element which is exist in repeating element
public	function single_element( & \$arr, \$size)
{
\$result = \$arr;
for (\$i = 1; \$i < \$size; ++\$i)
{
// Perform xor operation
\$result = \$result ^ \$arr[\$i];
}
\$this->display(\$arr, \$size);
// Display calculated result
echo "\n   Single element ". \$result ."\n\n";
}
}

function main()
{
\$find = new SingleElement();
// Arrays which is containing single and double(two times) repeating elements
\$arr1 = array(8, 7, 9, 5, 8, 9, 7);
\$arr2 = array(1, 1, 9, 5, 6, 6, 5, 4, 4);
// Get the size
\$size = count(\$arr1);
\$find->single_element(\$arr1, \$size);
// Get the size
\$size = count(\$arr2);
\$find->single_element(\$arr2, \$size);
}
main();``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9
``````
``````/*
Node Js Program
Find the element that appears once in array
*/
class SingleElement
{
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("   " + arr[i]);
}
}
// Finds the single appearing element which is exist in repeating element
single_element(arr, size)
{
var result = arr;
for (var i = 1; i < size; ++i)
{
// Perform xor operation
result = result ^ arr[i];
}
this.display(arr, size);
// Display calculated result
process.stdout.write("\n   Single element " + result + "\n\n");
}
}

function main()
{
var find = new SingleElement();
// Arrays which is containing single and double(two times) repeating elements
var arr1 = [8, 7, 9, 5, 8, 9, 7];
var arr2 = [1, 1, 9, 5, 6, 6, 5, 4, 4];
// Get the size
var size = arr1.length;
find.single_element(arr1, size);
// Get the size
size = arr2.length;
find.single_element(arr2, size);
}
main();``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9
``````
``````#   Python 3 Program
#   Find the element that appears once in array

class SingleElement :
# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

#  Finds the single appearing element which is exist in repeating element
def single_element(self, arr, size) :
result = arr
i = 1
while (i < size) :
#  Perform xor operation
result = result ^ arr[i]
i += 1

self.display(arr, size)
#  Display calculated result
print("\n   Single element ", result ,"\n")

def main() :
find = SingleElement()
#  Arrays which is containing single and double(two times) repeating elements
arr1 = [8, 7, 9, 5, 8, 9, 7]
arr2 = [1, 1, 9, 5, 6, 6, 5, 4, 4]
#  Get the size
size = len(arr1)
find.single_element(arr1, size)
#  Get the size
size = len(arr2)
find.single_element(arr2, size)

if __name__ == "__main__": main()``````

#### Output

``````   8   7   9   5   8   9   7
Single element  5

1   1   9   5   6   6   5   4   4
Single element  9
``````
``````#
#   Ruby Program
#   Find the element that appears once in array

class SingleElement
# Function which is display array elements
def display(arr, size)
i = 0
while (i < size)
print("   ", arr[i])
i += 1
end

end

#  Finds the single appearing element which is exist in repeating element
def single_element(arr, size)
result = arr
i = 1
while (i < size)
#  Perform xor operation
result = result ^ arr[i]
i += 1
end

self.display(arr, size)
#  Display calculated result
print("\n   Single element ", result ,"\n\n")
end

end

def main()
find = SingleElement.new()
#  Arrays which is containing single and double(two times) repeating elements
arr1 = [8, 7, 9, 5, 8, 9, 7]
arr2 = [1, 1, 9, 5, 6, 6, 5, 4, 4]
#  Get the size
size = arr1.length
find.single_element(arr1, size)
#  Get the size
size = arr2.length
find.single_element(arr2, size)
end

main()``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9

``````
``````/*
Scala Program
Find the element that appears once in array
*/
class SingleElement
{
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("   " + arr(i));
i += 1;
}
}
// Finds the single appearing element which is exist in repeating element
def single_element(arr: Array[Int], size: Int): Unit = {
var result: Int = arr(0);
var i: Int = 1;
while (i < size)
{
// Perform xor operation
result = result ^ arr(i);
i += 1;
}
this.display(arr, size);
// Display calculated result
print("\n   Single element " + result + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var find: SingleElement = new SingleElement();
// Arrays which is containing single and double(two times) repeating elements
var arr1: Array[Int] = Array(8, 7, 9, 5, 8, 9, 7);
var arr2: Array[Int] = Array(1, 1, 9, 5, 6, 6, 5, 4, 4);
// Get the size
var size: Int = arr1.length;
find.single_element(arr1, size);
// Get the size
size = arr2.length;
find.single_element(arr2, size);
}
}``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 9
``````
``````/*
Swift 4 Program
Find the element that appears once in array
*/
class SingleElement
{
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
// Finds the single appearing element which is exist in repeating element
func single_element(_ arr: [Int], _ size: Int)
{
var result: Int = arr;
var i: Int = 1;
while (i < size)
{
// Perform xor operation
result = result ^ arr[i];
i += 1;
}
self.display(arr, size);
// Display calculated result
print("\n   Single element ", result ,"\n");
}
}
func main()
{
let find: SingleElement = SingleElement();
// Arrays which is containing single and double(two times) repeating elements
let arr1: [Int] = [8, 7, 9, 5, 8, 9, 7];
let arr2: [Int] = [1, 1, 9, 5, 6, 6, 5, 4, 4];
// Get the size
var size: Int = arr1.count;
find.single_element(arr1, size);
// Get the size
size = arr2.count;
find.single_element(arr2, size);
}
main();``````

#### Output

``````   8   7   9   5   8   9   7
Single element  5

1   1   9   5   6   6   5   4   4
Single element  9
``````
``````/*
Kotlin Program
Find the element that appears once in array
*/
class SingleElement
{
//Function which is display array elements
fun display(arr: Array<Int>, size: Int): Unit
{
var i: Int = 0;
while (i<size)
{
print("   " + arr[i]);
i += 1;
}
}
// Finds the single appearing element which is exist in repeating element
fun single_element(arr: Array<Int>, size: Int): Unit
{
var result: Int = arr;
var i: Int = 1;
while (i<size)
{
// Perform xor operation
result = result xor arr[i];
i += 1;
}
this.display(arr, size);
// Display calculated result
print("\n   Single element " + result + "\n\n");
}
}
fun main(args: Array<String>): Unit
{
var find: SingleElement = SingleElement();
// Arrays which is containing single and double(two times) repeating elements
var arr1: Array<Int> = arrayOf(8, 7, 9, 5, 8, 9, 7);
var arr2: Array<Int> = arrayOf(1, 1, 9, 5, 6, 6, 5, 4, 4);
// Get the size
var size: Int = arr1.count();
find.single_element(arr1, size);
// Get the size
size = arr2.count();
find.single_element(arr2, size);
}``````

#### Output

``````   8   7   9   5   8   9   7
Single element 5

1   1   9   5   6   6   5   4   4
Single element 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 