Find the element that appears once in array Set B
Given of collection of integer elements. That is contains one single element and remaining elements are appears exactly three three times. Our goal is to find that single element which is single of this collection.
Here given code implementation process.
// C Program
// Find the element that appears once in array
// When other elements are exactly repeating of 3 times
#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 i = 0;
// Define useful resultant variables
// Get first element
int ones = 0;
int twos = 0;
int threes = 0;
//Execute Loop is from 0 ... size
for (i = 0; i < size; ++i)
{
twos = twos | (ones & arr[i]);
ones = ones ^ arr[i];
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
}
display(arr, size);
// Display calculated result
printf("\n Single element = %d \n", ones);
}
int main()
{
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
int arr1[] = {
1 , 2 , 3 , 8 , 1 , 4 , 2 , 4 , 1 , 3 , 2 , 3 , 4
};
int arr2[] = {
1 , 2 , 2 , 2 , 3 , 1 , 1 , 4 , 4 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
single_element(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
single_element(arr2, size);
return 0;
}
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
/*
Java Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i = 0;
// Define useful resultant variables
// Get first element
int ones = 0;
int twos = 0;
int threes = 0;
//Execute Loop is from 0... size
for (i = 0; i < size; ++i)
{
twos = twos | (ones & arr[i]);
ones = ones ^ arr[i];
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
}
display(arr, size);
// Display calculated result
System.out.print("\n Single element = " + ones + " \n");
}
public static void main(String[] args)
{
SingleElement s = new SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
int[] arr1 = {
1 , 2 , 3 , 8 , 1 , 4 , 2 , 4 , 1 , 3 , 2 , 3 , 4
};
int[] arr2 = {
1 , 2 , 2 , 2 , 3 , 1 , 1 , 4 , 4 , 4
};
// Get the size
int size = arr1.length;
s.single_element(arr1, size);
// Get the size
size = arr2.length;
s.single_element(arr2, size);
}
}
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i = 0;
// Define useful resultant variables
// Get first element
int ones = 0;
int twos = 0;
int threes = 0;
//Execute Loop is from 0... size
for (i = 0; i < size; ++i)
{
twos = twos | (ones & arr[i]);
ones = ones ^ arr[i];
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
}
this->display(arr, size);
// Display calculated result
cout << "\n Single element = " << ones << " \n";
}
};
int main()
{
SingleElement s = SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
int arr1[] = {
1 , 2 , 3 , 8 , 1 , 4 , 2 , 4 , 1 , 3 , 2 , 3 , 4
};
int arr2[] = {
1 , 2 , 2 , 2 , 3 , 1 , 1 , 4 , 4 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
s.single_element(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
s.single_element(arr2, size);
return 0;
}
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
// Include namespace system
using System;
/*
C# Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i = 0;
// Define useful resultant variables
// Get first element
int ones = 0;
int twos = 0;
int threes = 0;
//Execute Loop is from 0... size
for (i = 0; i < size; ++i)
{
twos = twos | (ones & arr[i]);
ones = ones ^ arr[i];
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
}
display(arr, size);
// Display calculated result
Console.Write("\n Single element = " + ones + " \n");
}
public static void Main(String[] args)
{
SingleElement s = new SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
int[] arr1 = {
1 , 2 , 3 , 8 , 1 , 4 , 2 , 4 , 1 , 3 , 2 , 3 , 4
};
int[] arr2 = {
1 , 2 , 2 , 2 , 3 , 1 , 1 , 4 , 4 , 4
};
// Get the size
int size = arr1.Length;
s.single_element(arr1, size);
// Get the size
size = arr2.Length;
s.single_element(arr2, size);
}
}
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
<?php
/*
Php Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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)
{
$i = 0;
// Define useful resultant variables
// Get first element
$ones = 0;
$twos = 0;
$threes = 0;
//Execute Loop is from 0... size
for ($i = 0; $i < $size; ++$i)
{
$twos = $twos | ($ones & $arr[$i]);
$ones = $ones ^ $arr[$i];
$threes = ($ones & $twos);
$ones = $ones & (~$threes);
$twos = $twos & (~$threes);
}
$this->display($arr, $size);
// Display calculated result
echo "\n Single element = ". $ones ." \n";
}
}
function main()
{
$s = new SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
$arr1 = array(1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4);
$arr2 = array(1, 2, 2, 2, 3, 1, 1, 4, 4, 4);
// Get the size
$size = count($arr1);
$s->single_element($arr1, $size);
// Get the size
$size = count($arr2);
$s->single_element($arr2, $size);
}
main();
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
/*
Node Js Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i = 0;
// Define useful resultant variables
// Get first element
var ones = 0;
var twos = 0;
var threes = 0;
//Execute Loop is from 0... size
for (i = 0; i < size; ++i)
{
twos = twos | (ones & arr[i]);
ones = ones ^ arr[i];
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
}
this.display(arr, size);
// Display calculated result
process.stdout.write("\n Single element = " + ones + " \n");
}
}
function main()
{
var s = new SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
var arr1 = [1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4];
var arr2 = [1, 2, 2, 2, 3, 1, 1, 4, 4, 4];
// Get the size
var size = arr1.length;
s.single_element(arr1, size);
// Get the size
size = arr2.length;
s.single_element(arr2, size);
}
main();
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
# Python 3 Program
# Find the element that appears once in array
# When other elements are exactly repeating of 3 times
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) :
i = 0
# Define useful resultant variables
# Get first element
ones = 0
twos = 0
threes = 0
# Execute Loop is from 0... size
while (i < size) :
twos = twos | (ones & arr[i])
ones = ones ^ arr[i]
threes = (ones & twos)
ones = ones & (~threes)
twos = twos & (~threes)
i += 1
self.display(arr, size)
# Display calculated result
print("\n Single element = ", ones ," ")
def main() :
s = SingleElement()
# Define Array which is containing one element occurrences single and
# Other elements are exist 3-3 times
arr1 = [1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4]
arr2 = [1, 2, 2, 2, 3, 1, 1, 4, 4, 4]
# Get the size
size = len(arr1)
s.single_element(arr1, size)
# Get the size
size = len(arr2)
s.single_element(arr2, size)
if __name__ == "__main__": main()
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
# Ruby Program
# Find the element that appears once in array
# When other elements are exactly repeating of 3 times
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)
i = 0
# Define useful resultant variables
# Get first element
ones = 0
twos = 0
threes = 0
# Execute Loop is from 0... size
while (i < size)
twos = twos | (ones & arr[i])
ones = ones ^ arr[i]
threes = (ones & twos)
ones = ones & (~threes)
twos = twos & (~threes)
i += 1
end
self.display(arr, size)
# Display calculated result
print("\n Single element = ", ones ," \n")
end
end
def main()
s = SingleElement.new()
# Define Array which is containing one element occurrences single and
# Other elements are exist 3-3 times
arr1 = [1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4]
arr2 = [1, 2, 2, 2, 3, 1, 1, 4, 4, 4]
# Get the size
size = arr1.length
s.single_element(arr1, size)
# Get the size
size = arr2.length
s.single_element(arr2, size)
end
main()
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
/*
Scala Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i: Int = 0;
// Define useful resultant variables
// Get first element
var ones: Int = 0;
var twos: Int = 0;
var threes: Int = 0;
//Execute Loop is from 0... size
while (i < size)
{
twos = twos | (ones & arr(i));
ones = ones ^ arr(i);
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
i += 1;
}
this.display(arr, size);
// Display calculated result
print("\n Single element = " + ones + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var s: SingleElement = new SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
var arr1: Array[Int] = Array(1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4);
var arr2: Array[Int] = Array(1, 2, 2, 2, 3, 1, 1, 4, 4, 4);
// Get the size
var size: Int = arr1.length;
s.single_element(arr1, size);
// Get the size
size = arr2.length;
s.single_element(arr2, size);
}
}
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
/*
Swift 4 Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i: Int = 0;
// Define useful resultant variables
// Get first element
var ones: Int = 0;
var twos: Int = 0;
var threes: Int = 0;
//Execute Loop is from 0... size
while (i < size)
{
twos = twos | (ones & arr[i]);
ones = ones ^ arr[i];
threes = (ones & twos);
ones = ones & (~threes);
twos = twos & (~threes);
i += 1;
}
self.display(arr, size);
// Display calculated result
print("\n Single element = ", ones ," ");
}
}
func main()
{
let s: SingleElement = SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
let arr1: [Int] = [1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4];
let arr2: [Int] = [1, 2, 2, 2, 3, 1, 1, 4, 4, 4];
// Get the size
var size: Int = arr1.count;
s.single_element(arr1, size);
// Get the size
size = arr2.count;
s.single_element(arr2, size);
}
main();
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
/*
Kotlin Program
Find the element that appears once in array
When other elements are exactly repeating of 3 times
*/
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 i: Int = 0;
// Define useful resultant variables
// Get first element
var ones: Int = 0;
var twos: Int = 0;
var threes: Int;
//Execute Loop is from 0... size
while (i<size)
{
twos = twos or(ones and arr[i]);
ones = ones xor arr[i];
threes = (ones and twos);
ones = ones and threes.inv();
twos = twos and threes.inv();
i += 1;
}
this.display(arr, size);
// Display calculated result
print("\n Single element = " + ones + " \n");
}
}
fun main(args: Array<String>): Unit
{
var s: SingleElement = SingleElement();
// Define Array which is containing one element occurrences single and
// Other elements are exist 3-3 times
var arr1: Array<Int> = arrayOf(1, 2, 3, 8, 1, 4, 2, 4, 1, 3, 2, 3, 4);
var arr2: Array<Int> = arrayOf(1, 2, 2, 2, 3, 1, 1, 4, 4, 4);
// Get the size
var size: Int = arr1.count();
s.single_element(arr1, size);
// Get the size
size = arr2.count();
s.single_element(arr2, size);
}
Output
1 2 3 8 1 4 2 4 1 3 2 3 4
Single element = 8
1 2 2 2 3 1 1 4 4 4
Single element = 3
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