Find the element that appears twice in array

Here given code implementation process.

// C Program
// Find the element that appears twice 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 elements of array which is appears of exactly twice
void twiceElement(int arr[], int size)
{
	if (size <= 0)
	{
		return;
	}
	display(arr, size);
	// Loop controlling variable
	int i = 0;
	int j = 0;
	// Element counter
	int counter = 0;
	int result = 0;
	printf("\nTwices element : ");
	for (i = 0; i < size - 1; ++i)
	{
		for (j = 0; j < size && counter <= 2; ++j)
		{
			if (arr[i] == arr[j])
			{
				if (j < i)
				{
					// If element has been already tested
					counter = 3;
				}
				else
				{
					counter++;
				}
			}
		}
		if (counter == 2)
		{
			result++;
			// Display element
			printf("  %d", arr[i]);
		}
		counter = 0;
	}
	if (result == 0)
	{
		printf(" None ");
	}
	printf("\n");
}
int main()
{
	//Define array elements
	int arr1[] = {
		6 , 11 , 1 , 3 , 6 , 1 , 32 , 8 , 4 , 8 , 1 , 7 , 12
	};
	int size = sizeof(arr1) / sizeof(arr1[0]);
	twiceElement(arr1, size);
	//Define array elements
	int arr2[] = {
		1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
	};
	size = sizeof(arr2) / sizeof(arr2[0]);
	twiceElement(arr2, size);
	return 0;
}

Output

6   11   1   3   6   1   32   8   4   8   1   7   12
Twices element :   6  8
1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
Twices element :   9  0  8  2
/*
  Java Program
  Find the element that appears twice in array
*/
public class TwiceAppearance
{
	//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 elements of array which is appears of exactly twice
	public void twiceElement(int[] arr, int size)
	{
		if (size <= 0)
		{
			return;
		}
		display(arr, size);
		// Loop controlling variable
		int i = 0;
		int j = 0;
		// Element counter
		int counter = 0;
		int result = 0;
		System.out.print("\nTwices element : ");
		for (i = 0; i < size - 1; ++i)
		{
			for (j = 0; j < size && counter <= 2; ++j)
			{
				if (arr[i] == arr[j])
				{
					if (j < i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter++;
					}
				}
			}
			if (counter == 2)
			{
				result++;
				// Display element
				System.out.print("  " + arr[i]);
			}
			counter = 0;
		}
		if (result == 0)
		{
			// When no result
			System.out.print(" None ");
		}
		System.out.print("\n\n");
	}
	public static void main(String[] args)
	{
		TwiceAppearance obj = new TwiceAppearance();
		//Define array elements
		int[] arr1 = {
			6 , 11 , 1 , 3 , 6 , 1, 32 , 8 , 4 , 8 , 1 , 7 , 12
		};
		int size = arr1.length;
		obj.twiceElement(arr1, size);
		//Define array elements
		int[] arr2 = {
			1 ,1 ,1 , 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9
		};
		size = arr2.length;
		obj.twiceElement(arr2, size);
	}
}

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
Twices element :   9  0  8  2
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Find the element that appears twice in array
*/
class TwiceAppearance
{
	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 elements of array which is appears of exactly twice
	void twiceElement(int arr[], int size)
	{
		if (size <= 0)
		{
			return;
		}
		this->display(arr, size);
		// Loop controlling variable
		int i = 0;
		int j = 0;
		// Element counter
		int counter = 0;
		int result = 0;
		cout << "\nTwices element : ";
		for (i = 0; i < size - 1; ++i)
		{
			for (j = 0; j < size && counter <= 2; ++j)
			{
				if (arr[i] == arr[j])
				{
					if (j < i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter++;
					}
				}
			}
			if (counter == 2)
			{
				result++;
				// Display element
				cout << "  " << arr[i];
			}
			counter = 0;
		}
		if (result == 0)
		{
			// When no result
			cout << " None ";
		}
		cout << "\n\n";
	}
};
int main()
{
	TwiceAppearance obj = TwiceAppearance();
	//Define array elements
	int arr1[] = {
		6 , 11 , 1 , 3 , 6 , 1 , 32 , 8 , 4 , 8 , 1 , 7 , 12
	};
	int size = sizeof(arr1) / sizeof(arr1[0]);
	obj.twiceElement(arr1, size);
	//Define array elements
	int arr2[] = {
		1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
	};
	size = sizeof(arr2) / sizeof(arr2[0]);
	obj.twiceElement(arr2, size);
	return 0;
}

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
Twices element :   9  0  8  2
// Include namespace system
using System;
/*
  C# Program
  Find the element that appears twice in array
*/
public class TwiceAppearance
{
	//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 elements of array which is appears of exactly twice
	public void twiceElement(int[] arr, int size)
	{
		if (size <= 0)
		{
			return;
		}
		display(arr, size);
		// Loop controlling variable
		int i = 0;
		int j = 0;
		// Element counter
		int counter = 0;
		int result = 0;
		Console.Write("\nTwices element : ");
		for (i = 0; i < size - 1; ++i)
		{
			for (j = 0; j < size && counter <= 2; ++j)
			{
				if (arr[i] == arr[j])
				{
					if (j < i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter++;
					}
				}
			}
			if (counter == 2)
			{
				result++;
				// Display element
				Console.Write("  " + arr[i]);
			}
			counter = 0;
		}
		if (result == 0)
		{
			// When no result
			Console.Write(" None ");
		}
		Console.Write("\n\n");
	}
	public static void Main(String[] args)
	{
		TwiceAppearance obj = new TwiceAppearance();
		//Define array elements
		int[] arr1 = {
			6 , 11 , 1 , 3 , 6 , 1 , 32 , 8 , 4 , 8 , 1 , 7 , 12
		};
		int size = arr1.Length;
		obj.twiceElement(arr1, size);
		//Define array elements
		int[] arr2 = {
			1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
		};
		size = arr2.Length;
		obj.twiceElement(arr2, size);
	}
}

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
Twices element :   9  0  8  2
<?php
/*
  Php Program
  Find the element that appears twice in array
*/
class TwiceAppearance
{
	//Function which is display array elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
	}
	// Finds the elements of array which is appears of exactly twice
	public	function twiceElement( & $arr, $size)
	{
		if ($size <= 0)
		{
			return;
		}
		$this->display($arr, $size);
		// Loop controlling variable
		$i = 0;
		$j = 0;
		// Element counter
		$counter = 0;
		$result = 0;
		echo "\nTwices element : ";
		for ($i = 0; $i < $size - 1; ++$i)
		{
			for ($j = 0; $j < $size && $counter <= 2; ++$j)
			{
				if ($arr[$i] == $arr[$j])
				{
					if ($j < $i)
					{
						// If element has been already tested
						$counter = 3;
					}
					else
					{
						$counter++;
					}
				}
			}
			if ($counter == 2)
			{
				$result++;
				// Display element
				echo "  ". $arr[$i];
			}
			$counter = 0;
		}
		if ($result == 0)
		{
			// When no result
			echo " None ";
		}
		echo "\n\n";
	}
}

function main()
{
	$obj = new TwiceAppearance();
	//Define array elements
	$arr1 = array(6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12);
	$size = count($arr1);
	$obj->twiceElement($arr1, $size);
	//Define array elements
	$arr2 = array(1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9);
	$size = count($arr2);
	$obj->twiceElement($arr2, $size);
}
main();

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
Twices element :   9  0  8  2
/*
  Node Js Program
  Find the element that appears twice in array
*/
class TwiceAppearance
{
	//Function which is display array elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
	}
	// Finds the elements of array which is appears of exactly twice
	twiceElement(arr, size)
	{
		if (size <= 0)
		{
			return;
		}
		this.display(arr, size);
		// Loop controlling variable
		var i = 0;
		var j = 0;
		// Element counter
		var counter = 0;
		var result = 0;
		process.stdout.write("\nTwices element : ");
		for (i = 0; i < size - 1; ++i)
		{
			for (j = 0; j < size && counter <= 2; ++j)
			{
				if (arr[i] == arr[j])
				{
					if (j < i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter++;
					}
				}
			}
			if (counter == 2)
			{
				result++;
				// Display element
				process.stdout.write("  " + arr[i]);
			}
			counter = 0;
		}
		if (result == 0)
		{
			// When no result
			process.stdout.write(" None ");
		}
		process.stdout.write("\n\n");
	}
}

function main()
{
	var obj = new TwiceAppearance();
	//Define array elements
	var arr1 = [6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12];
	var size = arr1.length;
	obj.twiceElement(arr1, size);
	//Define array elements
	var arr2 = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9];
	size = arr2.length;
	obj.twiceElement(arr2, size);
}
main();

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
Twices element :   9  0  8  2
#   Python 3 Program
#   Find the element that appears twice in array

class TwiceAppearance :
	# Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
	
	#  Finds the elements of array which is appears of exactly twice
	def twiceElement(self, arr, size) :
		if (size <= 0) :
			return
		
		self.display(arr, size)
		#  Loop controlling variable
		i = 0
		j = 0
		#  Element counter
		counter = 0
		result = 0
		print("\n  Twices element : ", end = "")
		i = 0
		while (i < size - 1) :
			j = 0
			while (j < size and counter <= 2) :
				if (arr[i] == arr[j]) :
					if (j < i) :
						#  If element has been already tested
						counter = 3
					else :
						counter += 1
					
				
				j += 1
			
			if (counter == 2) :
				result += 1
				#  Display element
				print("  ", arr[i], end = "")
			
			counter = 0
			i += 1
		
		if (result == 0) :
			#  When no result
			print(" None ", end = "")
		
		print("\n")
	

def main() :
	obj = TwiceAppearance()
	# Define array elements
	arr1 = [6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12]
	size = len(arr1)
	obj.twiceElement(arr1, size)
	# Define array elements
	arr2 = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9]
	size = len(arr2)
	obj.twiceElement(arr2, size)

if __name__ == "__main__": main()

Output

   6   11   1   3   6   1   32   8   4   8   1   7   12
  Twices element :    6   8

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
  Twices element :    9   0   8   2
#   Ruby Program
#   Find the element that appears twice in array

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

	end

	#  Finds the elements of array which is appears of exactly twice
	def twiceElement(arr, size) 
		if (size <= 0) 
			return
		end

		self.display(arr, size)
		#  Loop controlling variable
		i = 0
		j = 0
		#  Element counter
		counter = 0
		result = 0
		print("\n  Twices element : ")
		i = 0
		while (i < size - 1) 
			j = 0
			while (j < size && counter <= 2) 
				if (arr[i] == arr[j]) 
					if (j < i) 
						#  If element has been already tested
						counter = 3
					else 
						counter += 1
					end

				end

				j += 1
			end

			if (counter == 2) 
				result += 1
				#  Display element
				print("  ", arr[i])
			end

			counter = 0
			i += 1
		end

		if (result == 0) 
			#  When no result
			print(" None ")
		end

		print("\n\n")
	end

end

def main() 
	obj = TwiceAppearance.new()
	# Define array elements
	arr1 = [6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12]
	size = arr1.length
	obj.twiceElement(arr1, size)
	# Define array elements
	arr2 = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9]
	size = arr2.length
	obj.twiceElement(arr2, size)
end

main()

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
  Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
  Twices element :   9  0  8  2

/*
  Scala Program
  Find the element that appears twice in array
*/
class TwiceAppearance
{
	//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 elements of array which is appears of exactly twice
	def twiceElement(arr: Array[Int], size: Int): Unit = {
		if (size <= 0)
		{
			return;
		}
		this.display(arr, size);
		// Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		// Element counter
		var counter: Int = 0;
		var result: Int = 0;
		print("\n  Twices element : ");
		i = 0;
		while (i < size - 1)
		{
			j = 0;
			while (j < size && counter <= 2)
			{
				if (arr(i) == arr(j))
				{
					if (j < i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter += 1;
					}
				}
				j += 1;
			}
			if (counter == 2)
			{
				result += 1;
				// Display element
				print("  " + arr(i));
			}
			counter = 0;
			i += 1;
		}
		if (result == 0)
		{
			// When no result
			print(" None ");
		}
		print("\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: TwiceAppearance = new TwiceAppearance();
		//Define array elements
		var arr1: Array[Int] = Array(6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12);
		var size: Int = arr1.length;
		obj.twiceElement(arr1, size);
		//Define array elements
		var arr2: Array[Int] = Array(1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9);
		size = arr2.length;
		obj.twiceElement(arr2, size);
	}
}

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
  Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
  Twices element :   9  0  8  2
/*
  Swift 4 Program
  Find the element that appears twice in array
*/
class TwiceAppearance
{
	//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 elements of array which is appears of exactly twice
	func twiceElement(_ arr: [Int], _ size: Int)
	{
		if (size <= 0)
		{
			return;
		}
		self.display(arr, size);
		// Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		// Element counter
		var counter: Int = 0;
		var result: Int = 0;
		print("\n  Twices element : ", terminator: "");
		i = 0;
		while (i < size - 1)
		{
			j = 0;
			while (j < size && counter <= 2)
			{
				if (arr[i] == arr[j])
				{
					if (j < i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter += 1;
					}
				}
				j += 1;
			}
			if (counter == 2)
			{
				result += 1;
				// Display element
				print("  ", arr[i], terminator: "");
			}
			counter = 0;
			i += 1;
		}
		if (result == 0)
		{
			// When no result
			print(" None ", terminator: "");
		}
		print("\n");
	}
}
func main()
{
	let obj: TwiceAppearance = TwiceAppearance();
	//Define array elements
	let arr1: [Int] = [6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12];
	var size: Int = arr1.count;
	obj.twiceElement(arr1, size);
	//Define array elements
	let arr2: [Int] = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9];
	size = arr2.count;
	obj.twiceElement(arr2, size);
}
main();

Output

   6   11   1   3   6   1   32   8   4   8   1   7   12
  Twices element :    6   8

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
  Twices element :    9   0   8   2
/*
  Kotlin Program
  Find the element that appears twice in array
*/
class TwiceAppearance
{
	//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 elements of array which is appears of exactly twice
	fun twiceElement(arr: Array<Int>, size: Int): Unit
	{
		if (size <= 0)
		{
			return;
		}
		this.display(arr, size);
		// Loop controlling variable
		var i: Int = 0;
		var j: Int ;
		// Element counter
		var counter: Int = 0;
		var result: Int = 0;
		print("\n  Twices element : ");
		while (i  < size - 1)
		{
			j = 0;
			while (j < size && counter <= 2)
			{
				if (arr[i] == arr[j])
				{
					if (j<i)
					{
						// If element has been already tested
						counter = 3;
					}
					else
					{
						counter += 1;
					}
				}
				j += 1;
			}
			if (counter == 2)
			{
				result += 1;
				// Display element
				print("  " + arr[i]);
			}
			counter = 0;
			i += 1;
		}
		if (result == 0)
		{
			// When no result
			print(" None ");
		}
		print("\n\n");
	}
}
fun main(args: Array<String>): Unit
{
	var obj: TwiceAppearance = TwiceAppearance();
	//Define array elements
	var arr1: Array<Int> = arrayOf(6, 11, 1, 3, 6, 1, 32, 8, 4, 8, 1, 7, 12);
	var size: Int = arr1.count();
	obj.twiceElement(arr1, size);
	//Define array elements
	var arr2: Array<Int> = arrayOf(1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9);
	size = arr2.count();
	obj.twiceElement(arr2, size);
}

Output

  6  11  1  3  6  1  32  8  4  8  1  7  12
  Twices element :   6  8

  1  1  1  6  9  0  8  8  0  2  5  7  2  4  9
  Twices element :   9  0  8  2


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







© 2021, kalkicode.com, All rights reserved