Skip to main content

Find the two duplicate elements in array

Given an array of numbers (integer values), Our goal is to find the first two duplicate element which is repeating in this array. For example

Example 1
arr[] = {1,7,2,4,4,4,3,6,4,3 }
o/p : 4  3
Example 2
arr[] = {1,1,1,4,3,3,3,3,4,3 }
o/p : 1  4

Here given code implementation process.

// C Program
// Find the two duplicate elements in array
#include <stdio.h>

//Function which is display array elements
void display(int arr[], int size)
{
    for (int i = 0; i < size; ++i)
    {
        printf("%d   ", arr[i]);
    }
}
// Find two repeated elements of array
void findDuplicate(int arr[], int size)
{
    if (size <= 0)
    {
        return;
    }
    display(arr, size);
    // Loop controlling variable
    int i = 0;
    int j = 0;
    int result = 0;
    int counter = 0;
    //
    int a = -1;
    int b = -1;
    for (i = 0; i < size - 1 && result < 2; ++i)
    {
        for (j = 0; j < size && counter >= 0 && counter < 2; ++j)
        {
            if (arr[i] == arr[j])
            {
                if (j < i)
                {
                    // If element has been already tested
                    counter = -1;
                }
                else
                {
                    counter++;
                }
            }
        }
        if (counter >= 2)
        {
            result++;
            if (a == -1)
            {
                a = i;
            }
            else if (b == -1)
            {
                b = i;
            }
        }
        counter = 0;
    }
    if (result == 0)
    {
        printf("\nNo duplicate elements exists ");
    }
    else
    {
        if (a != -1)
        {
            printf("\nFirst repeated : %d", arr[a]);
        }
        if (b != -1)
        {
            printf("\nSecond repeated : %d", arr[b]);
        }
        else
        {
            printf("\nSecond repeated : None");
        }
    }
    printf("\n\n");
}
int main()
{
    //Define array elements
    int arr1[] = {
        1 , 4 , 7 , 7 , 7 , 9 , 11 , 5 , 8 , 5
    };
    int size = sizeof(arr1) / sizeof(arr1[0]);
    findDuplicate(arr1, size);
    //Define array elements
    int arr2[] = {
        1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
    };
    size = sizeof(arr2) / sizeof(arr2[0]);
    findDuplicate(arr2, size);
    return 0;
}

Output

1   4   7   7   7   9   11   5   8   5
First repeated : 7
Second repeated : 5

1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
First repeated : 1
Second repeated : 9
/*
  Java Program
  Find the two duplicate elements in array
*/
public class DuplicateElement
{
    //Function which is display array elements
    public void display(int[] arr, int size)
    {
        for (int i = 0; i < size; ++i)
        {
            System.out.print("   " + arr[i]);
        }
    }
    // Find two repeated elements of array
    public void findDuplicate(int[] arr, int size)
    {
        if (size <= 0)
        {
            return;
        }
        display(arr, size);
        // Loop controlling variables
        int i = 0;
        int j = 0;
        // Counter variables
        int result = 0;
        int counter = 0;
        // Resultant variabless
        int a = -1;
        int b = -1;
        for (i = 0; i < size - 1 && result < 2; ++i)
        {
            for (j = 0; j < size && counter >= 0 && counter < 2; ++j)
            {
                if (arr[i] == arr[j])
                {
                    if (j < i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter++;
                    }
                }
            }
            if (counter >= 2)
            {
                result++;
                if (a == -1)
                {
                    a = i;
                }
                else if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
        }
        if (result == 0)
        {
            System.out.print("\n    No duplicate elements exists ");
        }
        else
        {
            if (a != -1)
            {
                System.out.print("\n    First repeated : " + arr[a] );
            }
            if (b != -1)
            {
                System.out.print("\n    Second repeated : " + arr[b]);
            }
            else
            {
                System.out.print("\n    Second repeated : None");
            }
        }
        System.out.print("\n\n");
    }
    public static void main(String[] args)
    {
        DuplicateElement obj = new DuplicateElement();
        //Define array elements
        int[] arr1 = {
            1 , 4 , 7 , 7 , 7 , 9 , 11 , 5 , 8 , 5
        };
        int size = arr1.length;
        obj.findDuplicate(arr1, size);
        //Define array elements
        int[] arr2 = {
            1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
        };
        size = arr2.length;
        obj.findDuplicate(arr2, size);
    }
}

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program
  Find the two duplicate elements in array
*/

class DuplicateElement
{
    public:
        //Function which is display array elements
        void display(int arr[], int size)
        {
            for (int i = 0; i < size; ++i)
            {
                cout << "   " << arr[i];
            }
        }
    // Find two repeated elements of array
    void findDuplicate(int arr[], int size)
    {
        if (size <= 0)
        {
            return;
        }
        this->display(arr, size);
        // Loop controlling variables
        int i = 0;
        int j = 0;
        // Counter variables
        int result = 0;
        int counter = 0;
        // Resultant variabless
        int a = -1;
        int b = -1;
        for (i = 0; i < size - 1 && result < 2; ++i)
        {
            for (j = 0; j < size && counter >= 0 && counter < 2; ++j)
            {
                if (arr[i] == arr[j])
                {
                    if (j < i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter++;
                    }
                }
            }
            if (counter >= 2)
            {
                result++;
                if (a == -1)
                {
                    a = i;
                }
                else if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
        }
        if (result == 0)
        {
            cout << "\n   No duplicate elements exists ";
        }
        else
        {
            if (a != -1)
            {
                cout << "\n   First repeated : " << arr[a];
            }
            if (b != -1)
            {
                cout << "\n   Second repeated : " << arr[b];
            }
            else
            {
                cout << "\n   Second repeated : None";
            }
        }
        cout << "\n\n";
    }
};
int main()
{
    DuplicateElement obj = DuplicateElement();
    //Define array elements
    int arr1[] = {
        1 , 4 , 7 , 7 , 7 , 9 , 11 , 5 , 8 , 5
    };
    int size = sizeof(arr1) / sizeof(arr1[0]);
    obj.findDuplicate(arr1, size);
    //Define array elements
    int arr2[] = {
        1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
    };
    size = sizeof(arr2) / sizeof(arr2[0]);
    obj.findDuplicate(arr2, size);
    return 0;
}

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9
// Include namespace system
using System;
/*
  C# Program
  Find the two duplicate elements in array
*/
public class DuplicateElement
{
    //Function which is display array elements
    public void display(int[] arr, int size)
    {
        for (int i = 0; i < size; ++i)
        {
            Console.Write("   " + arr[i]);
        }
    }
    // Find two repeated elements of array
    public void findDuplicate(int[] arr, int size)
    {
        if (size <= 0)
        {
            return;
        }
        display(arr, size);
        // Loop controlling variables
        int i = 0;
        int j = 0;
        // Counter variables
        int result = 0;
        int counter = 0;
        // Resultant variabless
        int a = -1;
        int b = -1;
        for (i = 0; i < size - 1 && result < 2; ++i)
        {
            for (j = 0; j < size && counter >= 0 && counter < 2; ++j)
            {
                if (arr[i] == arr[j])
                {
                    if (j < i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter++;
                    }
                }
            }
            if (counter >= 2)
            {
                result++;
                if (a == -1)
                {
                    a = i;
                }
                else if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
        }
        if (result == 0)
        {
            Console.Write("\n   No duplicate elements exists ");
        }
        else
        {
            if (a != -1)
            {
                Console.Write("\n   First repeated : " + arr[a]);
            }
            if (b != -1)
            {
                Console.Write("\n   Second repeated : " + arr[b]);
            }
            else
            {
                Console.Write("\n   Second repeated : None");
            }
        }
        Console.Write("\n\n");
    }
    public static void Main(String[] args)
    {
        DuplicateElement obj = new DuplicateElement();
        //Define array elements
        int[] arr1 = {
            1 , 4 , 7 , 7 , 7 , 9 , 11 , 5 , 8 , 5
        };
        int size = arr1.Length;
        obj.findDuplicate(arr1, size);
        //Define array elements
        int[] arr2 = {
            1 , 1 , 1 , 6 , 9 , 0 , 8 , 8 , 0 , 2 , 5 , 7 , 2 , 4 , 9
        };
        size = arr2.Length;
        obj.findDuplicate(arr2, size);
    }
}

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9
<?php
/*
  Php Program
  Find the two duplicate elements in array
*/
class DuplicateElement
{
    //Function which is display array elements
    public  function display( & $arr, $size)
    {
        for ($i = 0; $i < $size; ++$i)
        {
            echo "   ". $arr[$i];
        }
    }
    // Find two repeated elements of array
    public  function findDuplicate( & $arr, $size)
    {
        if ($size <= 0)
        {
            return;
        }
        $this->display($arr, $size);
        // Loop controlling variables
        $i = 0;
        $j = 0;
        // Counter variables
        $result = 0;
        $counter = 0;
        // Resultant variabless
        $a = -1;
        $b = -1;
        for ($i = 0; $i < $size - 1 && $result < 2; ++$i)
        {
            for ($j = 0; $j < $size && $counter >= 0 && $counter < 2; ++$j)
            {
                if ($arr[$i] == $arr[$j])
                {
                    if ($j < $i)
                    {
                        // If element has been already tested
                        $counter = -1;
                    }
                    else
                    {
                        $counter++;
                    }
                }
            }
            if ($counter >= 2)
            {
                $result++;
                if ($a == -1)
                {
                    $a = $i;
                }
                else if ($b == -1)
                {
                    $b = $i;
                }
            }
            $counter = 0;
        }
        if ($result == 0)
        {
            echo "\n    No duplicate elements exists ";
        }
        else
        {
            if ($a != -1)
            {
                echo "\n    First repeated : ". $arr[$a];
            }
            if ($b != -1)
            {
                echo "\n    Second repeated : ". $arr[$b];
            }
            else
            {
                echo "\n    Second repeated : None";
            }
        }
        echo "\n\n";
    }
}

function main()
{
    $obj = new DuplicateElement();
    //Define array elements
    $arr1 = array(1, 4, 7, 7, 7, 9, 11, 5, 8, 5);
    $size = count($arr1);
    $obj->findDuplicate($arr1, $size);
    //Define array elements
    $arr2 = array(1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9);
    $size = count($arr2);
    $obj->findDuplicate($arr2, $size);
}
main();

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9
/*
  Node Js Program
  Find the two duplicate elements in array
*/
class DuplicateElement
{
    //Function which is display array elements
    display(arr, size)
    {
        for (var i = 0; i < size; ++i)
        {
            process.stdout.write("   " + arr[i]);
        }
    }
    // Find two repeated elements of array
    findDuplicate(arr, size)
    {
        if (size <= 0)
        {
            return;
        }
        this.display(arr, size);
        // Loop controlling variables
        var i = 0;
        var j = 0;
        // Counter variables
        var result = 0;
        var counter = 0;
        // Resultant variabless
        var a = -1;
        var b = -1;
        for (i = 0; i < size - 1 && result < 2; ++i)
        {
            for (j = 0; j < size && counter >= 0 && counter < 2; ++j)
            {
                if (arr[i] == arr[j])
                {
                    if (j < i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter++;
                    }
                }
            }
            if (counter >= 2)
            {
                result++;
                if (a == -1)
                {
                    a = i;
                }
                else if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
        }
        if (result == 0)
        {
            process.stdout.write("\n    No duplicate elements exists ");
        }
        else
        {
            if (a != -1)
            {
                process.stdout.write("\n    First repeated : " + arr[a]);
            }
            if (b != -1)
            {
                process.stdout.write("\n    Second repeated : " + arr[b]);
            }
            else
            {
                process.stdout.write("\n    Second repeated : None");
            }
        }
        process.stdout.write("\n\n");
    }
}

function main()
{
    var obj = new DuplicateElement();
    //Define array elements
    var arr1 = [1, 4, 7, 7, 7, 9, 11, 5, 8, 5];
    var size = arr1.length;
    obj.findDuplicate(arr1, size);
    //Define array elements
    var arr2 = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9];
    size = arr2.length;
    obj.findDuplicate(arr2, size);
}
main();

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9
#   Python 3 Program
#   Find the two duplicate elements in array

class DuplicateElement :
    # Function which is display array elements
    def display(self, arr, size) :
        i = 0
        while (i < size) :
            print("   ", arr[i], end = "")
            i += 1
        
    
    #  Find two repeated elements of array
    def findDuplicate(self, arr, size) :
        if (size <= 0) :
            return
        
        self.display(arr, size)
        #  Loop controlling variables
        i = 0
        j = 0
        #  Counter variables
        result = 0
        counter = 0
        #  Resultant variabless
        a = -1
        b = -1
        i = 0
        while (i < size - 1 and result < 2) :
            j = 0
            while (j < size and counter >= 0 and counter < 2) :
                if (arr[i] == arr[j]) :
                    if (j < i) :
                        #  If element has been already tested
                        counter = -1
                    else :
                        counter += 1
                    
                
                j += 1
            
            if (counter >= 2) :
                result += 1
                if (a == -1) :
                    a = i
                
                elif(b == -1) :
                    b = i
                
            
            counter = 0
            i += 1
        
        if (result == 0) :
            print("\n   No duplicate elements exists ", end = "")
        else :
            if (a != -1) :
                print("\n   First repeated : ", arr[a], end = "")
            
            if (b != -1) :
                print("\n   Second repeated : ", arr[b], end = "")
            else :
                print("\n   Second repeated : None", end = "")
            
        
        print("\n")
    

def main() :
    obj = DuplicateElement()
    # Define array elements
    arr1 = [1, 4, 7, 7, 7, 9, 11, 5, 8, 5]
    size = len(arr1)
    obj.findDuplicate(arr1, size)
    # Define array elements
    arr2 = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9]
    size = len(arr2)
    obj.findDuplicate(arr2, size)

if __name__ == "__main__": main()

Output

    1    4    7    7    7    9    11    5    8    5
    First repeated :  7
    Second repeated :  5

    1    1    1    6    9    0    8    8    0    2    5    7    2    4    9
    First repeated :  1
    Second repeated :  9
#   Ruby Program
#   Find the two duplicate elements in array

class DuplicateElement 
    # Function which is display array elements
    def display(arr, size) 
        i = 0
        while (i < size) 
            print("   ", arr[i])
            i += 1
        end
    end

    #  Find two repeated elements of array
    def findDuplicate(arr, size) 
        if (size <= 0) 
            return
        end

        self.display(arr, size)
        #  Loop controlling variables
        i = 0
        j = 0
        #  Counter variables
        result = 0
        counter = 0
        #  Resultant variabless
        a = -1
        b = -1
        i = 0
        while (i < size - 1 && result < 2) 
            j = 0
            while (j < size && counter >= 0 && counter < 2) 
                if (arr[i] == arr[j]) 
                    if (j < i) 
                        #  If element has been already tested
                        counter = -1
                    else 
                        counter += 1
                    end

                end

                j += 1
            end

            if (counter >= 2) 
                result += 1
                if (a == -1) 
                    a = i
                elsif(b == -1) 
                    b = i
                end

            end

            counter = 0
            i += 1
        end

        if (result == 0) 
            print("\n   No duplicate elements exists ")
        else 
            if (a != -1) 
                print("\n   First repeated : ", arr[a])
            end

            if (b != -1) 
                print("\n   Second repeated : ", arr[b])
            else 
                print("\n   Second repeated : None")
            end

        end

        print("\n\n")
    end

end

def main() 
    obj = DuplicateElement.new()
    # Define array elements
    arr1 = [1, 4, 7, 7, 7, 9, 11, 5, 8, 5]
    size = arr1.length
    obj.findDuplicate(arr1, size)
    # Define array elements
    arr2 = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9]
    size = arr2.length
    obj.findDuplicate(arr2, size)
end

main()

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9

/*
  Scala Program
  Find the two duplicate elements in array
*/
class DuplicateElement
{
    //Function which is display array elements
    def display(arr: Array[Int], size: Int): Unit = {
        var i: Int = 0;
        while (i < size)
        {
            print("   " + arr(i));
            i += 1;
        }
    }
    // Find two repeated elements of array
    def findDuplicate(arr: Array[Int], size: Int): Unit = {
        if (size <= 0)
        {
            return;
        }
        this.display(arr, size);
        // Loop controlling variables
        var i: Int = 0;
        var j: Int = 0;
        // Counter variables
        var result: Int = 0;
        var counter: Int = 0;
        // Resultant variabless
        var a: Int = -1;
        var b: Int = -1;
        i = 0;
        while (i < size - 1 && result < 2)
        {
            j = 0;
            while (j < size && counter >= 0 && counter < 2)
            {
                if (arr(i) == arr(j))
                {
                    if (j < i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter += 1;
                    }
                }
                j += 1;
            }
            if (counter >= 2)
            {
                result += 1;
                if (a == -1)
                {
                    a = i;
                }
                else if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
            i += 1;
        }
        if (result == 0)
        {
            print("\n   No duplicate elements exists ");
        }
        else
        {
            if (a != -1)
            {
                print("\n   First repeated : " + arr(a));
            }
            if (b != -1)
            {
                print("\n   Second repeated : " + arr(b));
            }
            else
            {
                print("\n   Second repeated : None");
            }
        }
        print("\n\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var obj: DuplicateElement = new DuplicateElement();
        //Define array elements
        var arr1: Array[Int] = Array(1, 4, 7, 7, 7, 9, 11, 5, 8, 5);
        var size: Int = arr1.length;
        obj.findDuplicate(arr1, size);
        //Define array elements
        var arr2: Array[Int] = Array(1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9);
        size = arr2.length;
        obj.findDuplicate(arr2, size);
    }
}

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9
/*
  Swift 4 Program
  Find the two duplicate elements in array
*/
class DuplicateElement
{
    //Function which is display array elements
    func display(_ arr: [Int], _ size: Int)
    {
        var i: Int = 0;
        while (i < size)
        {
            print("   ", arr[i], terminator: "");
            i += 1;
        }
    }
    // Find two repeated elements of array
    func findDuplicate(_ arr: [Int], _ size: Int)
    {
        if (size <= 0)
        {
            return;
        }
        self.display(arr, size);
        // Loop controlling variables
        var i: Int = 0;
        var j: Int = 0;
        // Counter variables
        var result: Int = 0;
        var counter: Int = 0;
        // Resultant variabless
        var a: Int = -1;
        var b: Int = -1;
        while (i < size - 1 && result < 2)
        {
            j = 0;
            while (j < size && counter >= 0 && counter < 2)
            {
                if (arr[i] == arr[j])
                {
                    if (j < i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter += 1;
                    }
                }
                j += 1;
            }
            if (counter >= 2)
            {
                result += 1;
                if (a == -1)
                {
                    a = i;
                }
                else if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
            i += 1;
        }
        if (result == 0)
        {
            print("\n  No duplicate elements exists ",terminator: "");
        }
        else
        {
            if (a != -1)
            {
                print("\n   First repeated : ", arr[a], terminator: "");
            }
            if (b != -1)
            {
                print("\n   Second repeated : ", arr[b], terminator: "");
            }
            else
            {
                print("\n   Second repeated : None", terminator: "");
            }
        }
        print("\n");
    }
}
func main()
{
    let obj: DuplicateElement = DuplicateElement();
    //Define array elements
    let arr1: [Int] = [1, 4, 7, 7, 7, 9, 11, 5, 8, 5];
    var size: Int = arr1.count;
    obj.findDuplicate(arr1, size);
    //Define array elements
    let arr2: [Int] = [1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9];
    size = arr2.count;
    obj.findDuplicate(arr2, size);
}
main();

Output

    1    4    7    7    7    9    11    5    8    5
   First repeated :  7
   Second repeated :  5

    1    1    1    6    9    0    8    8    0    2    5    7    2    4    9
   First repeated :  1
   Second repeated :  9
/*
  Kotlin Program
  Find the two duplicate elements in array
*/
class DuplicateElement
{
    //Function which is display array elements
    fun display(arr: Array<Int>, size: Int): Unit
    {
        var i: Int = 0;
        while (i<size)
        {
            print("   " + arr[i]);
            i += 1;
        }
    }
    // Find two repeated elements of array
    fun findDuplicate(arr: Array<Int>, size: Int): Unit
    {
        if (size <= 0)
        {
            return;
        }
        this.display(arr, size);
        // Loop controlling variables
        var i: Int = 0;
        var j: Int ;
        // Counter variables
        var result: Int = 0;
        var counter: Int = 0;
        // Resultant variabless
        var a: Int = -1;
        var b: Int = -1;
        while (i<size - 1 && result<2)
        {
            j = 0;
            while (j<size && counter>= 0 && counter<2)
            {
                if (arr[i] == arr[j])
                {
                    if (j<i)
                    {
                        // If element has been already tested
                        counter = -1;
                    }
                    else
                    {
                        counter += 1;
                    }
                }
                j += 1;
            }
            if (counter>= 2)
            {
                result += 1;
                if (a == -1)
                {
                    a = i;
                }
                else
                if (b == -1)
                {
                    b = i;
                }
            }
            counter = 0;
            i += 1;
        }
        if (result == 0)
        {
            print("\n   No duplicate elements exists ");
        }
        else
        {
            if (a != -1)
            {
                print("\n   First repeated : " + arr[a]);
            }
            if (b != -1)
            {
                print("\n   Second repeated : " + arr[b]);
            }
            else
            {
                print("\n   Second repeated : None");
            }
        }
        print("\n\n");
    }
}
fun main(args: Array<String>): Unit
{
    var obj: DuplicateElement = DuplicateElement();
    //Define array elements
    var arr1: Array<Int> = arrayOf(1, 4, 7, 7, 7, 9, 11, 5, 8, 5);
    var size: Int = arr1.count();
    obj.findDuplicate(arr1, size);
    //Define array elements
    var arr2: Array<Int> = arrayOf(1, 1, 1, 6, 9, 0, 8, 8, 0, 2, 5, 7, 2, 4, 9);
    size = arr2.count();
    obj.findDuplicate(arr2, size);
}

Output

   1   4   7   7   7   9   11   5   8   5
    First repeated : 7
    Second repeated : 5

   1   1   1   6   9   0   8   8   0   2   5   7   2   4   9
    First repeated : 1
    Second repeated : 9




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