General Java JavaScript Php Python

Find duplicates in an array

Explanation 

We need to find the duplicate elements in the array in this programme. This can be accomplished with two loops. The first loop selects an element, while the second loop iterates across the array by comparing the selected element to other elements. If a duplicate element is detected, print it. 

123427883

The first duplication in the above array will be discovered at index 4, which is a duplicate of the element (2) at index 1. In the above array, the duplicate entries are 2, 3, and 8.

Algorithm 

  • Create and populate an array. 
  • Two loops can be used to find duplicate components. The outer loop iterates across the array from 0 to the array’s length. The element will be chosen by the outer loop. The selected element will be compared to the rest of the array’s elements using the inner loop. 
  • If a match is detected, it signifies the duplicate element has been located, so show it.

Solution

Python

#Initialize array   
arr = [1, 2, 3, 4, 2, 7, 8, 8, 3];   
   
print("Duplicate elements in given array: ");  
#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]); 

Output:

Duplicate elements in given array: 
2
3
8

C

#include<iostream>   
   
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;  
}  

Output:

Duplicate elements in given array: 
2
3
8

JAVA

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]);  
            }  
        }  
    }  
}

Output:

Duplicate elements in given array: 
2
3
8

C#

using System;  
                      
public class DuplicateElement  
{  
    public static void Main()  
    {  
        //Initialize array   
        int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};   
          
       Console.WriteLine("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])  
                    Console.WriteLine(arr[j]);  
            }  
        }  
    }  
}

Output:

Duplicate elements in given array: 
2
3
8

PHP

");  
//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] . "
");  
    }  
}  
?>  

Output:

Duplicate elements in given array: 
2
3
8

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *