Skip to main content

Find all pairs in array

Here given code implementation process.

// C Program 
// Find all pairs in array
#include <stdio.h>

// Print the all possible pairs which is exists in given collection
void allPairs(char collection[], int size)
{
	// Outer loop through by number of elements in collection
	for (int i = 0; i < size; ++i)
	{
		// Inner loop (0...size)
		for (int j = 0; j < size; ++j)
		{
			// Display pair
			printf(" (%c,%c) ", collection[i], collection[j]);
		}
		printf("\n");
	}
}
int main(int argc, char const *argv[])
{
	// Define character elements
	char collection[] = {
		'A' , 'B' , 'C' , 'D'
	};
	// Get the size
	int size = sizeof(collection) / sizeof(collection[0]);
	// Test
	allPairs(collection, size);
	return 0;
}

Output

 (A,A)  (A,B)  (A,C)  (A,D)
 (B,A)  (B,B)  (B,C)  (B,D)
 (C,A)  (C,B)  (C,C)  (C,D)
 (D,A)  (D,B)  (D,C)  (D,D)
/*
  Java Program for
  Find all pairs in array
*/
class Pairs
{
	// Print the all possible pairs which is exists in given collection
	public void allPairs(char[] collection)
	{
		int size = collection.length;
		// Outer loop through by number of elements in collection
		for (int i = 0; i < size; ++i)
		{
			// Inner loop (0...size)
			for (int j = 0; j < size; ++j)
			{
				// Display pair
				System.out.print(" (" + collection[i] + "," + collection[j] + ")");
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Pairs task = new Pairs();
		// Define character elements
		char[] collection = {
			'A' , 'B' , 'C' , 'D'
		};
		task.allPairs(collection);
	}
}

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find all pairs in array
*/
class Pairs
{
	public:
		// Print the all possible pairs which is exists in given collection
		void allPairs(char collection[], int size)
		{
			// Outer loop through by number of elements in collection
			for (int i = 0; i < size; ++i)
			{
				// Inner loop (0...size)
				for (int j = 0; j < size; ++j)
				{
					// Display pair
					cout << " (" << collection[i] << "," << collection[j] << ")";
				}
				cout << "\n";
			}
		}
};
int main()
{
	Pairs task = Pairs();
	// Define character elements
	char collection[] = {
		'A' , 'B' , 'C' , 'D'
	};
  	int size = sizeof(collection) / sizeof(collection[0]);
	task.allPairs(collection,size);
	return 0;
}

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
// Include namespace system
using System;
/*
  C# Program for
  Find all pairs in array
*/
public class Pairs
{
	// Print the all possible pairs which is exists in given collection
	public void allPairs(char[] collection)
	{
		int size = collection.Length;
		// Outer loop through by number of elements in collection
		for (int i = 0; i < size; ++i)
		{
			// Inner loop (0...size)
			for (int j = 0; j < size; ++j)
			{
				// Display pair
				Console.Write(" (" + collection[i] + "," + collection[j] + ")");
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pairs task = new Pairs();
		// Define character elements
		char[] collection = {
			'A' , 'B' , 'C' , 'D'
		};
		task.allPairs(collection);
	}
}

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
<?php
/*
  Php Program for
  Find all pairs in array
*/
class Pairs
{
	// Print the all possible pairs which is exists in given collection
	public	function allPairs( & $collection)
	{
		$size = count($collection);
		// Outer loop through by number of elements in collection
		for ($i = 0; $i < $size; ++$i)
		{
			// Inner loop (0...size)
			for ($j = 0; $j < $size; ++$j)
			{
				// Display pair
				echo " (". $collection[$i] .",". $collection[$j] .")";
			}
			echo "\n";
		}
	}
}

function main()
{
	$task = new Pairs();
	// Define character elements
	$collection = array('A', 'B', 'C', 'D');
	$task->allPairs($collection);
}
main();

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
/*
  Node Js Program for
  Find all pairs in array
*/
class Pairs
{
	// Print the all possible pairs which is exists in given collection
	allPairs(collection)
	{
		var size = collection.length;
		// Outer loop through by number of elements in collection
		for (var i = 0; i < size; ++i)
		{
			// Inner loop (0...size)
			for (var j = 0; j < size; ++j)
			{
				// Display pair
				process.stdout.write(" (" + collection[i] + "," + collection[j] + ")");
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Pairs();
	// Define character elements
	var collection = ['A', 'B', 'C', 'D'];
	task.allPairs(collection);
}
main();

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
#   Python 3 Program for
#   Find all pairs in array

class Pairs :
	#  Print the all possible pairs which is exists in given collection
	def allPairs(self, collection) :
		#  Get the size
		size = len(collection)
		#  Loop controlling variable	
		i = 0
		j = 0
		#  Outer loop through by number of elements in collection
		while (i < size) :
			#  Inner loop (0...size)
			while (j < size) :
				#  Display pair
				print(" (", collection[i] ,",", collection[j] ,")", end = "")
				j += 1
			
			print(end = "\n")
			#  Change position
			i += 1
			j = 0
		
	

def main() :
	task = Pairs()
	#  Define character elements
	collection = ['A', 'B', 'C', 'D']
	task.allPairs(collection)

if __name__ == "__main__": main()

Output

 ( A , A ) ( A , B ) ( A , C ) ( A , D )
 ( B , A ) ( B , B ) ( B , C ) ( B , D )
 ( C , A ) ( C , B ) ( C , C ) ( C , D )
 ( D , A ) ( D , B ) ( D , C ) ( D , D )
#   Ruby Program for
#   Find all pairs in array

class Pairs 
	#  Print the all possible pairs which is exists in given collection
	def allPairs(collection) 
		#  Get the size
		size = collection.length
		#  Loop controlling variable	
		i = 0
		j = 0
		#  Outer loop through by number of elements in collection
		while (i < size) 
			#  Inner loop (0...size)
			while (j < size) 
				#  Display pair
				print(" (", collection[i] ,",", collection[j] ,")")
				j += 1
			end

			print("\n")
			#  Change position
			i += 1
			j = 0
		end

	end

end

def main() 
	task = Pairs.new()
	#  Define character elements
	collection = ['A', 'B', 'C', 'D']
	task.allPairs(collection)
end

main()

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
/*
  Scala Program for
  Find all pairs in array
*/
class Pairs
{
	// Print the all possible pairs which is exists in given collection
	def allPairs(collection: Array[Character]): Unit = {
		// Get the size
		var size: Int = collection.length;
		// Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		// Outer loop through by number of elements in collection
		while (i < size)
		{
			// Inner loop (0...size)
			while (j < size)
			{
				// Display pair
				print(" (" + collection(i) + "," + collection(j) + ")");
				j += 1;
			}
			print("\n");
			// Change position
			i += 1;
			j = 0;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pairs = new Pairs();
		// Define character elements
		var collection: Array[Character] = Array('A', 'B', 'C', 'D');
		task.allPairs(collection);
	}
}

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)
/*
  Swift 4 Program for
  Find all pairs in array
*/
class Pairs
{
	// Print the all possible pairs which is exists in given collection
	func allPairs(_ collection: [Character])
	{
		// Get the size
		let size: Int = collection.count;
		// Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		// Outer loop through by number of elements in collection
		while (i < size)
		{
			// Inner loop (0...size)
			while (j < size)
			{
				// Display pair
				print(" (", collection[i] ,",", collection[j] ,")", terminator: "");
				j += 1;
			}
			print(terminator: "\n");
			// Change position
			i += 1;
			j = 0;
		}
	}
}
func main()
{
	let task: Pairs = Pairs();
	// Define character elements
	let collection: [Character] = ["A", "B", "C", "D"];
	task.allPairs(collection);
}
main();

Output

 ( A , A ) ( A , B ) ( A , C ) ( A , D )
 ( B , A ) ( B , B ) ( B , C ) ( B , D )
 ( C , A ) ( C , B ) ( C , C ) ( C , D )
 ( D , A ) ( D , B ) ( D , C ) ( D , D )
/*
  Kotlin Program for
  Find all pairs in array
*/
class Pairs
{
	// Print the all possible pairs which is exists in given collection
	fun allPairs(collection: Array < Char > ): Unit
	{
		// Get the size
		var size: Int = collection.count();
		// Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		// Outer loop through by number of elements in collection
		while (i < size)
		{
			// Inner loop (0...size)
			while (j < size)
			{
				// Display pair
				print(" (" + collection[i] + "," + collection[j] + ")");
				j += 1;
			}
			print("\n");
			// Change position
			i += 1;
			j = 0;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Pairs = Pairs();
	// Define character elements
	var collection: Array < Char > = arrayOf('A', 'B', 'C', 'D');
	task.allPairs(collection);
}

Output

 (A,A) (A,B) (A,C) (A,D)
 (B,A) (B,B) (B,C) (B,D)
 (C,A) (C,B) (C,C) (C,D)
 (D,A) (D,B) (D,C) (D,D)




Comment

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