Last active
August 24, 2020 11:40
-
-
Save Khant-Nyar/ca4c7a0d9d3307fe7a91fa1617f4d9ca to your computer and use it in GitHub Desktop.
Q. Program to print the duplicate elements of an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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