Skip to content

Instantly share code, notes, and snippets.

@Khant-Nyar
Last active August 24, 2020 11:40
Show Gist options
  • Save Khant-Nyar/ca4c7a0d9d3307fe7a91fa1617f4d9ca to your computer and use it in GitHub Desktop.
Save Khant-Nyar/ca4c7a0d9d3307fe7a91fa1617f4d9ca to your computer and use it in GitHub Desktop.
Q. Program to print the duplicate elements of an array
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 2, 7, 8, 8, 3};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
printf("Duplicate elements in given array: \n");
//Searches for duplicate element
for(int i = 0; i < length; i++) {
for(int j = i + 1; j < length; j++) {
if(arr[i] == arr[j])
printf("%d\n", arr[j]);
}
}
return 0;
}
public class DuplicateElement {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
System.out.println("Duplicate elements in given array: ");
//Searches for duplicate element
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] == arr[j])
System.out.println(arr[j]);
}
}
}
}
<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array
$arr = array(1, 2, 3, 4, 2, 7, 8, 8, 3);
print("Duplicate elements in given array: <br>");
//Searches for duplicate element
for($i = 0; $i < count($arr); $i++) {
for($j = $i + 1; $j < count($arr); $j++) {
if($arr[$i] == $arr[$j])
print($arr[$j] . "<br>");
}
}
?>
</body>
</html>
#Q. Program to print the duplicate elements of an array
# Algorithm
# Declare and initialize an array.
# Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.
# If a match is found which means the duplicate element is found then, display the element.
# Solution
# Python
#Initialize array
arr = ['a','b','c','a','b','b','d','c','f','c','g','c',];
#b=3,a=2,c=4
print("My Duplicate Array is : ['a','b','c','a','b','b','d','c','f','c','g','c',] ");
#Searches for duplicate element
for i in range(0, len(arr)):
for j in range(i+1, len(arr)):
if(arr[i] == arr[j]):
print(arr[j]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment