Skip to main content

Find duplicate elements in given array

Here given code implementation process.

/*
    Java program for
    Find duplicate elements in given array
*/
import java.util.HashMap;
public class Duplicate
{
	// Display elements of given array
	public void displayArray(int[] num, int n)
	{
		System.out.print("\n Array elements : ");
		for (int i = 0; i < n; ++i)
		{
			System.out.print("  " + num[i]);
		}
	}
	public void findDuplicateElement(int[] num, int n)
	{
		// Use to collect frequency of array elements
		HashMap < Integer, Integer > frequency = 
          new HashMap < Integer, Integer > ();
		boolean result = false;
		// Count frequency of num elements
		for (int i = 0; i < n; i++)
		{
			if (frequency.containsKey(num[i]))
			{
				// When key exist then put new key
				frequency.put(num[i], frequency.get(num[i]) + 1);
			}
			else
			{
				frequency.put(num[i], 1);
			}
		}
		// Display given elements
		displayArray(num, n);
		System.out.print("\n Duplicate : ");
		// Display duplicate elements
		for (int data: frequency.keySet())
		{
			if (frequency.get(data) > 1)
			{
				// When element are duplicate
				System.out.print("  " + data);
				result = true;
			}
		}
		if (result == false)
		{
			System.out.print(" None");
		}
	}
	public static void main(String[] args)
	{
		Duplicate task = new Duplicate();
		int[] num1 = {
			5 , -4 , 1 , 4 , 6 , -7 , 1 , 6 , 3 , -4
		};
		int[] num2 = {
			2 , 5 , 1 , 3 , 3 , 2 
		};
		int[] num3 = {
			9 , 3 , 5
		};
		// Test A
		int n = num1.length;
		task.findDuplicateElement(num1, n);
		// Test B
		n = num2.length;
		task.findDuplicateElement(num2, n);
		// Test C
		n = num3.length;
		task.findDuplicateElement(num3, n);
	}
}

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   1  -4  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
// Include header file
#include <iostream>
#include <unordered_map>

using namespace std;
class Duplicate
{
	public:
		// Display elements of given array
		void displayArray(int num[], int n)
		{
			cout << "\n Array elements : ";
			for (int i = 0; i < n; ++i)
			{
				cout << "  " << num[i];
			}
		}
	void findDuplicateElement(int num[], int n)
	{
		// Use to collect frequency of array elements
		unordered_map < int, int > frequency;
		bool result = false;
		// Count frequency of num elements
		for (int i = 0; i < n; i++)
		{
			if (frequency.find(num[i]) != frequency.end())
			{
				// When key exist then put new key
				frequency[num[i]] = frequency[num[i]] + 1;
			}
			else
			{
				frequency[num[i]] = 1;
			}
		}
		// Display given elements
		this->displayArray(num, n);
		cout << "\n Duplicate : ";
		// Display duplicate elements
		for (auto &data: frequency)
		{
			if (data.second > 1)
			{
				// When element are duplicate
				cout << "  " << data.first;
				result = true;
			}
		}
		if (result == false)
		{
			cout << " None";
		}
	}
};
int main()
{
	Duplicate *task = new Duplicate();
	int num1[] = {
		5 , -4 , 1 , 4 , 6 , -7 , 1 , 6 , 3 , -4
	};
	int num2[] = {
		2 , 5 , 1 , 3 , 3 , 2
	};
	int num3[] = {
		9 , 3 , 5
	};
	// Test A
	int n = sizeof(num1) / sizeof(num1[0]);
	task->findDuplicateElement(num1, n);
	// Test B
	n = sizeof(num2) / sizeof(num2[0]);
	task->findDuplicateElement(num2, n);
	// Test C
	n = sizeof(num3) / sizeof(num3[0]);
	task->findDuplicateElement(num3, n);
	return 0;
}

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   6  -4  1
 Array elements :   2  5  1  3  3  2
 Duplicate :   3  2
 Array elements :   9  3  5
 Duplicate :  None
// Include namespace system
using System;
using System.Collections.Generic;
public class Duplicate
{
	// Display elements of given array
	public void displayArray(int[] num, int n)
	{
		Console.Write("\n Array elements : ");
		for (int i = 0; i < n; ++i)
		{
			Console.Write("  " + num[i]);
		}
	}
	public void findDuplicateElement(int[] num, int n)
	{
		// Use to collect frequency of array elements
		Dictionary < int, int > frequency = new Dictionary < int, int > ();
		Boolean result = false;
		// Count frequency of num elements
		for (int i = 0; i < n; i++)
		{
			if (frequency.ContainsKey(num[i]))
			{
				// When key exist then put new key
				frequency[num[i]] = frequency[num[i]] + 1;
			}
			else
			{
				frequency.Add(num[i], 1);
			}
		}
		// Display given elements
		this.displayArray(num, n);
		Console.Write("\n Duplicate : ");
		// Display duplicate elements
		foreach(KeyValuePair < int, int > data in frequency)
		{
			if (data.Value > 1)
			{
				// When element are duplicate
				Console.Write("  " + data.Key);
				result = true;
			}
		}
		if (result == false)
		{
			Console.Write(" None");
		}
	}
	public static void Main(String[] args)
	{
		Duplicate task = new Duplicate();
		int[] num1 = {
			5 , -4 , 1 , 4 , 6 , -7 , 1 , 6 , 3 , -4
		};
		int[] num2 = {
			2 , 5 , 1 , 3 , 3 , 2
		};
		int[] num3 = {
			9 , 3 , 5
		};
		// Test A
		int n = num1.Length;
		task.findDuplicateElement(num1, n);
		// Test B
		n = num2.Length;
		task.findDuplicateElement(num2, n);
		// Test C
		n = num3.Length;
		task.findDuplicateElement(num3, n);
	}
}

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   -4  1  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
package main
import "fmt"
/*
    Go program for
    Find duplicate elements in given array
*/

// Display elements of given array
func displayArray(num[] int, n int) {
	fmt.Print("\n Array elements : ")
	for i := 0 ; i < n ; i++ {
		fmt.Print("  ", num[i])
	}
}
func findDuplicateElement(num[] int, n int) {
	// Use to collect frequency of array elements
	var frequency = make(map[int] int)
	var result bool = false
	// Count frequency of num elements
	for i := 0 ; i < n ; i++ {
		if _, found := frequency[num[i]] ; found {
			// When key exist then put new key
			frequency[num[i]] = frequency[num[i]] + 1
		} else {
			frequency[num[i]] = 1
		}
	}
	// Display given elements
	displayArray(num, n)
	fmt.Print("\n Duplicate : ")
	// Display duplicate elements
	for k, v := range frequency {
		if v > 1 {
			// When element are duplicate
			fmt.Print("  ", k)
			result = true
		}
	}
	if result == false {
		fmt.Print(" None")
	}
}
func main() {

	var num1 = [] int {5 , -4 , 1 , 4 , 6 , -7 , 1 , 6 , 3 , -4}
	var num2 = [] int { 2 , 3 , 5 , 1 , 3 , 2}
	var num3 = [] int {9 , 3 , 5}
	// Test A
	var n int = len(num1)
	findDuplicateElement(num1, n)
	// Test B
	n = len(num2)
	findDuplicateElement(num2, n)
	// Test C
	n = len(num3)
	findDuplicateElement(num3, n)
}

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   1  -4  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
<?php
/*
    Php program for
    Find duplicate elements in given array
*/
class Duplicate
{
	// Display elements of given array
	public	function displayArray($num, $n)
	{
		echo("\n Array elements : ");
		for ($i = 0; $i < $n; ++$i)
		{
			echo("  ".$num[$i]);
		}
	}
	public	function findDuplicateElement($num, $n)
	{
		// Use to collect frequency of array elements
		$frequency = array();
		$result = false;
		// Count frequency of num elements
		for ($i = 0; $i < $n; $i++)
		{
			if (array_key_exists($num[$i], $frequency))
			{
				// When key exist then put new key
				$frequency[$num[$i]] = $frequency[$num[$i]] + 1;
			}
			else
			{
				$frequency[$num[$i]] = 1;
			}
		}
		// Display given elements
		$this->displayArray($num, $n);
		echo("\n Duplicate : ");
		// Display duplicate elements
		foreach($frequency as $key => $value)
		{
			if ($value > 1)
			{
				// When element are duplicate
				echo("  ".$key);
				$result = true;
			}
		}
		if ($result == false)
		{
			echo(" None");
		}
	}
}

function main()
{
	$task = new Duplicate();
	$num1 = array(5, -4, 1, 4, 6, -7, 1, 6, 3, -4);
	$num2 = array(2, 5, 1, 3, 3, 2);
	$num3 = array(9, 3, 5);
	// Test A
	$n = count($num1);
	$task->findDuplicateElement($num1, $n);
	// Test B
	$n = count($num2);
	$task->findDuplicateElement($num2, $n);
	// Test C
	$n = count($num3);
	$task->findDuplicateElement($num3, $n);
}
main();

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   -4  1  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
/*
    Node JS program for
    Find duplicate elements in given array
*/
class Duplicate
{
	// Display elements of given array
	displayArray(num, n)
	{
		process.stdout.write("\n Array elements : ");
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write("  " + num[i]);
		}
	}
	findDuplicateElement(num, n)
	{
		// Use to collect frequency of array elements
		var frequency = new Map();
		var result = false;
		// Count frequency of num elements
		for (var i = 0; i < n; i++)
		{
			if (frequency.has(num[i]))
			{
				// When key exist then put new key
				frequency.set(num[i], frequency.get(num[i]) + 1);
			}
			else
			{
				frequency.set(num[i], 1);
			}
		}
		// Display given elements
		this.displayArray(num, n);
		process.stdout.write("\n Duplicate : ");
		// Display duplicate elements
		for (let [key, value] of frequency)
		{
			if (value > 1)
			{
				// When element are duplicate
				process.stdout.write("  " + key);
				result = true;
			}
		}
		if (result == false)
		{
			process.stdout.write(" None");
		}
	}
}

function main()
{
	var task = new Duplicate();
	var num1 = [5, -4, 1, 4, 6, -7, 1, 6, 3, -4];
	var num2 = [2, 5, 1, 3, 3, 2];
	var num3 = [9, 3, 5];
	// Test A
	var n = num1.length;
	task.findDuplicateElement(num1, n);
	// Test B
	n = num2.length;
	task.findDuplicateElement(num2, n);
	// Test C
	n = num3.length;
	task.findDuplicateElement(num3, n);
}
main();

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   -4  1  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
#    Python 3 program for
#    Find duplicate elements in given array
class Duplicate :
	#  Display elements of given list
	def displayArray(self, num, n) :
		print("\n Array elements : ", end = "")
		i = 0
		while (i < n) :
			print("  ", num[i], end = "")
			i += 1
		
	
	def findDuplicateElement(self, num, n) :
		#  Use to collect frequency of list elements
		frequency = dict()
		result = False
		i = 0
		#  Count frequency of num elements
		while (i < n) :
			if ((num[i] in frequency.keys())) :
				#  When key exist then put new key
				frequency[num[i]] = frequency.get(num[i]) + 1
			else :
				frequency[num[i]] = 1
			
			i += 1
		
		#  Display given elements
		self.displayArray(num, n)
		print("\n Duplicate : ", end = "")
		for key, value in frequency.items() :
			if (value > 1) :
				#  When element are duplicate
				print("  ", key, end = "")
				result = True
			
		
		if (result == False) :
			print(" None", end = "")
		
	

def main() :
	task = Duplicate()
	num1 = [5, -4, 1, 4, 6, -7, 1, 6, 3, -4]
	num2 = [2, 5, 1, 3, 3, 2]
	num3 = [9, 3, 5]
	#  Test A
	n = len(num1)
	task.findDuplicateElement(num1, n)
	#  Test B
	n = len(num2)
	task.findDuplicateElement(num2, n)
	#  Test C
	n = len(num3)
	task.findDuplicateElement(num3, n)

if __name__ == "__main__": main()

Output

 Array elements :    5   -4   1   4   6   -7   1   6   3   -4
 Duplicate :    1   6   -4
 Array elements :    2   5   1   3   3   2
 Duplicate :    2   3
 Array elements :    9   3   5
 Duplicate :  None
#    Ruby program for
#    Find duplicate elements in given array
class Duplicate 
	#  Display elements of given array
	def displayArray(num, n) 
		print("\n Array elements : ")
		i = 0
		while (i < n) 
			print("  ", num[i])
			i += 1
		end

	end

	def findDuplicateElement(num, n) 
		#  Use to collect frequency of array elements
		frequency = Hash.new()
		result = false
		i = 0
		#  Count frequency of num elements
		while (i < n) 
			if (frequency.key?(num[i])) 
				#  When key exist then put new key
				frequency[num[i]] = frequency[num[i]] + 1
			else
 
				frequency[num[i]] = 1
			end

			i += 1
		end

		#  Display given elements
		self.displayArray(num, n)
		print("\n Duplicate : ")
		#  Display duplicate elements
		frequency.each { | key, value |
			if (value > 1) 
				#  When element are duplicate
				print("  ", key)
				result = true
			end

		}
		if (result == false) 
			print(" None")
		end

	end

end

def main() 
	task = Duplicate.new()
	num1 = [5, -4, 1, 4, 6, -7, 1, 6, 3, -4]
	num2 = [2, 5, 1, 3, 3, 2]
	num3 = [9, 3, 5]
	#  Test A
	n = num1.length
	task.findDuplicateElement(num1, n)
	#  Test B
	n = num2.length
	task.findDuplicateElement(num2, n)
	#  Test C
	n = num3.length
	task.findDuplicateElement(num3, n)
end

main()

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   -4  1  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
import scala.collection.mutable._;
/*
    Scala program for
    Find duplicate elements in given array
*/
class Duplicate()
{
	// Display elements of given array
	def displayArray(num: Array[Int], n: Int): Unit = {
		print("\n Array elements : ");
		var i: Int = 0;
		while (i < n)
		{
			print("  " + num(i));
			i += 1;
		}
	}
	def findDuplicateElement(num: Array[Int], n: Int): Unit = {
		// Use to collect frequency of array elements
		var frequency: HashMap[Int, Int] = new HashMap[Int, Int]();
		var result: Boolean = false;
		var i: Int = 0;
		// Count frequency of num elements
		while (i < n)
		{
			if (frequency.contains(num(i)))
			{
				// When key exist then put new key
				frequency.addOne(num(i), frequency.get(num(i)).get + 1);
			}
			else
			{
				frequency.addOne(num(i), 1);
			}
			i += 1;
		}
		// Display given elements
		displayArray(num, n);
		print("\n Duplicate : ");
		// Display duplicate elements
		for ((key, value) <- frequency)
		{
			if (value > 1)
			{
				// When element are duplicate
				print("  " + key);
				result = true;
			}
		}
		if (result == false)
		{
			print(" None");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Duplicate = new Duplicate();
		var num1: Array[Int] = Array(5, -4, 1, 4, 6, -7, 1, 6, 3, -4);
		var num2: Array[Int] = Array(2, 5, 1, 3, 3, 2);
		var num3: Array[Int] = Array(9, 3, 5);
		// Test A
		var n: Int = num1.length;
		task.findDuplicateElement(num1, n);
		// Test B
		n = num2.length;
		task.findDuplicateElement(num2, n);
		// Test C
		n = num3.length;
		task.findDuplicateElement(num3, n);
	}
}

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   1  -4  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None
import Foundation;
/*
    Swift 4 program for
    Find duplicate elements in given array
*/
class Duplicate
{
	// Display elements of given array
	func displayArray(_ num: [Int], _ n: Int)
	{
		print("\n Array elements : ", terminator: "");
		var i: Int = 0;
		while (i < n)
		{
			print("  ", num[i], terminator: "");
			i += 1;
		}
	}
	func findDuplicateElement(_ num: [Int], _ n: Int)
	{
		// Use to collect frequency of array elements
		var frequency = [Int: Int]();
		var result: Bool = false;
		var i: Int = 0;
		// Count frequency of num elements
		while (i < n)
		{
			if (frequency.keys.contains(num[i]))
			{
				// When key exist then put new key
				frequency[num[i]] = frequency[num[i]]! + 1;
			}
			else
			{
				frequency[num[i]] = 1;
			}
			i += 1;
		}
		// Display given elements
		self.displayArray(num, n);
		print("\n Duplicate : ", terminator: "");
		// Display duplicate elements
		for (key, value) in frequency
		{
			if (value > 1)
			{
				// When element are duplicate
				print("  ", key, terminator: "");
				result = true;
			}
		}
		if (result == false)
		{
			print(" None", terminator: "");
		}
	}
}
func main()
{
	let task: Duplicate = Duplicate();
	let num1: [Int] = [5, -4, 1, 4, 6, -7, 1, 6, 3, -4];
	let num2: [Int] = [2, 5, 1, 3, 3, 2];
	let num3: [Int] = [9, 3, 5];
	// Test A
	var n: Int = num1.count;
	task.findDuplicateElement(num1, n);
	// Test B
	n = num2.count;
	task.findDuplicateElement(num2, n);
	// Test C
	n = num3.count;
	task.findDuplicateElement(num3, n);
}
main();

Output

 Array elements :    5   -4   1   4   6   -7   1   6   3   -4
 Duplicate :    -4   6   1
 Array elements :    2   5   1   3   3   2
 Duplicate :    2   3
 Array elements :    9   3   5
 Duplicate :  None
/*
    Kotlin program for
    Find duplicate elements in given array
*/
class Duplicate
{
	// Display elements of given array
	fun displayArray(num: Array < Int > , n: Int): Unit
	{
		print("\n Array elements : ");
		var i: Int = 0;
		while (i < n)
		{
			print("  " + num[i]);
			i += 1;
		}
	}
	fun findDuplicateElement(num: Array < Int > , n: Int): Unit
	{
		// Use to collect frequency of array elements
		val frequency = HashMap < Int , Int > ();
		var result: Boolean = false;
		var i: Int = 0;
		// Count frequency of num elements
		while (i < n)
		{
			if (frequency.containsKey(num[i]))
			{
				// When key exist then put new key
				frequency.put(num[i], frequency.getValue(num[i]) + 1);
			}
			else
			{
				frequency.put(num[i], 1);
			}
			i += 1;
		}
		// Display given elements
		this.displayArray(num, n);
		print("\n Duplicate : ");
		// Display duplicate elements
		for ((key, value) in frequency)
		{
			if (value > 1)
			{
				// When element are duplicate
				print("  " + key);
				result = true;
			}
		}
		if (result == false)
		{
			print(" None");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Duplicate = Duplicate();
	val num1: Array < Int > = arrayOf(5, -4, 1, 4, 6, -7, 1, 6, 3, -4);
	val num2: Array < Int > = arrayOf(2, 5, 1, 3, 3, 2);
	val num3: Array < Int > = arrayOf(9, 3, 5);
	// Test A
	var n: Int = num1.count();
	task.findDuplicateElement(num1, n);
	// Test B
	n = num2.count();
	task.findDuplicateElement(num2, n);
	// Test C
	n = num3.count();
	task.findDuplicateElement(num3, n);
}

Output

 Array elements :   5  -4  1  4  6  -7  1  6  3  -4
 Duplicate :   1  -4  6
 Array elements :   2  5  1  3  3  2
 Duplicate :   2  3
 Array elements :   9  3  5
 Duplicate :  None




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