Find common elements in three unsorted arrays

Given of three unsorted array of integer elements, Our goal is to find the distinct common node which is exist in given arrays. For example.

Input 3 Arrays
arr1[] = {4 , 1 , 3 , 7 , 8 , 2 , 3 , 9 , 5 , -1 , -1 , -1}
arr2[] = {8 , 5 , 1 , 6 , 8 , 3 , 9 , 6 , 3 , 5 , 6}
arr3[] = {5 , 7 , 2 , 4 , 7 , 9 , 7 , 8 , 1 , 1 , 8 , 6 , 6 , 6}

OutPut :  1  5  8  9 (distinct common) 

Here given code implementation process.

import java.util.HashMap;
// Java Program 
// Find common elements in three unsorted arrays
public class SimilarElements
{
    public void findCommon(int[] arr1, int[] arr2, 
            int[] arr3, int a, int b, int c)
    {
        // This are used to collect frequency of array elements
        HashMap < Integer, Integer > record = 
          new HashMap < Integer, Integer > ();
        boolean result = false;
        // Collect distinct elements in first array
        for (int i = 0; i < a; ++i)
        {
            if (!record.containsKey(arr1[i]))
            {
                record.put(arr1[i], 1);
            }
        }
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        for (int i = 0; i < b; ++i)
        {
            if (record.containsKey(arr2[i]) && record.get(arr2[i]) == 1)
            {
                record.put(arr2[i], 2);
            }
        }
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        for (int i = 0; i < c; ++i)
        {
            if (record.containsKey(arr3[i]) && record.get(arr3[i]) == 2)
            {
                record.put(arr3[i], 3);
            }
        }
        // Display all common elements
        for (int info: record.keySet())
        {
            if (record.get(info) == 3)
            {
                result = true;
                System.out.print("  " + info);
            }
        }
        if (result == false)
        {
            System.out.print(" None ");
        }
    }
    public static void main(String args[])
    {
        SimilarElements task = new SimilarElements();
        int[] arr1 = {
            4 , 1 , 3 , 7 , 8 , 2 , 3 , 9 , 5 , -1 , -1 , -1
        };
        int[] arr2 = {
            8 , 5 , 1 , 6 , 8 , 3 , 9 , 6 , 3 , 5 , 6
        };
        int[] arr3 = {
            5 , 7 , 2 , 4 , 7 , 9 , 7 , 8 , 1 , 1 , 8 , 6 , 6 , 6
        };
        // Get the size
        int a = arr1.length;
        int b = arr2.length;
        int c = arr3.length;
        // Test
        task.findCommon(arr1, arr2, arr3, a, b, c);
    }
}

Output

  1  5  8  9
// Include header file
#include <iostream>
#include <map>

using namespace std;
// C++ Program 
// Find common elements in three unsorted arrays
class SimilarElements
{
    public: void findCommon(int arr1[], int arr2[], 
            int arr3[], int a, int b, int c)
    {
        // This are used to collect frequency of array elements
        map < int, int > record;
        bool result = false;
        // Collect distinct elements in first array
        for (int i = 0; i < a; ++i)
        {
            if (record.find(arr1[i]) == record.end())
            {
                record[arr1[i]] = 1;
            }
        }
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        for (int i = 0; i < b; ++i)
        {
            if (record.find(arr2[i]) != record.end() && 
                record[arr2[i]] == 1)
            {
                record[arr2[i]] = 2;
            }
        }
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        for (int i = 0; i < c; ++i)
        {
            if (record.find(arr3[i]) != record.end() && 
                record[arr3[i]] == 2)
            {
                record[arr3[i]] = 3;
            }
        }
        // Display all common elements
        for (auto &info: record)
        {
            if (record[info.first] == 3)
            {
                result = true;
                cout << "  " << info.first;
            }
        }
        if (result == false)
        {
            cout << " None ";
        }
    }
};
int main()
{
    SimilarElements *task = new SimilarElements();
    int arr1[] = {
        4 , 1 , 3 , 7 , 8 , 2 , 3 , 9 , 5 , -1 , -1 , -1
    };
    int arr2[] = {
        8 , 5 , 1 , 6 , 8 , 3 , 9 , 6 , 3 , 5 , 6
    };
    int arr3[] = {
        5 , 7 , 2 , 4 , 7 , 9 , 7 , 8 , 1 , 1 , 8 , 6 , 6 , 6
    };
    // Get the size
    int a = sizeof(arr1) / sizeof(arr1[0]);
    int b = sizeof(arr2) / sizeof(arr2[0]);
    int c = sizeof(arr3) / sizeof(arr3[0]);
    // Test
    task->findCommon(arr1, arr2, arr3, a, b, c);
    return 0;
}

Output

  1  5  8  9
// Include namespace system
using System;
using System.Collections.Generic;
// Csharp Program 
// Find common elements in three unsorted arrays
public class SimilarElements
{
    public void findCommon(int[] arr1, int[] arr2, 
            int[] arr3, int a, int b, int c)
    {
        // This are used to collect frequency of array elements
        Dictionary < int, int > record = new Dictionary < int, int > ();
        Boolean result = false;
        // Collect distinct elements in first array
        for (int i = 0; i < a; ++i)
        {
            if (!record.ContainsKey(arr1[i]))
            {
                record.Add(arr1[i], 1);
            }
        }
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        for (int i = 0; i < b; ++i)
        {
            if (record.ContainsKey(arr2[i]) && record[arr2[i]] == 1)
            {
                record[arr2[i]] = 2;
            }
        }
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        for (int i = 0; i < c; ++i)
        {
            if (record.ContainsKey(arr3[i]) && record[arr3[i]] == 2)
            {
                record[arr3[i]] = 3;
            }
        }
        // Display all common elements
        foreach(KeyValuePair < int, int > info in record)
        {
            if (info.Value == 3)
            {
                result = true;
                Console.Write("  " + info.Key);
            }
        }
        if (result == false)
        {
            Console.Write(" None ");
        }
    }
    public static void Main(String[] args)
    {
        SimilarElements task = new SimilarElements();
        int[] arr1 = {
            4 , 1 , 3 , 7 , 8 , 2 , 3 , 9 , 5 , -1 , -1 , -1
        };
        int[] arr2 = {
            8 , 5 , 1 , 6 , 8 , 3 , 9 , 6 , 3 , 5 , 6
        };
        int[] arr3 = {
            5 , 7 , 2 , 4 , 7 , 9 , 7 , 8 , 1 , 1 , 8 , 6 , 6 , 6
        };
        // Get the size
        int a = arr1.Length;
        int b = arr2.Length;
        int c = arr3.Length;
        // Test
        task.findCommon(arr1, arr2, arr3, a, b, c);
    }
}

Output

  1  8  9  5
package main
import "fmt"
// Go Program 
// Find common elements in three unsorted arrays
type SimilarElements struct {}
func getSimilarElements() * SimilarElements {
    var me *SimilarElements = &SimilarElements {}
    return me
}
func(this SimilarElements) findCommon(arr1[] int, arr2[] int, 
    arr3[] int, a int, b int, c int) {
    // This are used to collect frequency of array elements
    var record = make(map[int] int)
    var result bool = false
    // Collect distinct elements in first array
    for i := 0 ; i < a ; i++ {
        if _, found := record[arr1[i]] ; !found {
            record[arr1[i]] = 1
        }
    }
    // Set the element frequency 2 that is present in the 
    // first and second arrays
    for i := 0 ; i < b ; i++ {
        if _, found := record[arr2[i]] ; found && record[arr2[i]] == 1 {
            record[arr2[i]] = 2
        }
    }
    // Set the element frequency 3 that is present in the 
    // first second and third arrays
    for i := 0 ; i < c ; i++ {
        if _, found := record[arr3[i]] ; found && record[arr3[i]] == 2 {
            record[arr3[i]] = 3
        }
    }
    // Display all common elements
    for k, v := range record {
        if v == 3 {
            result = true
            fmt.Print("  ", k)
        }
    }
    if result == false {
        fmt.Print(" None ")
    }
}
func main() {
    var task * SimilarElements = getSimilarElements()
    var arr1 = [] int {4 , 1 , 3 , 7 , 8 , 2 , 3 , 9 , 5 , -1 , -1 , -1}
    var arr2 = [] int {8 , 5 , 1 , 6 , 8 , 3 , 9 , 6 , 3 , 5 , 6}
    var arr3 = [] int {5 , 7 , 2 , 4 , 7 , 9 , 7 , 8 , 1 , 1 , 8 , 6 , 6 , 6}
    // Get the size
    var a int = len(arr1)
    var b int = len(arr2)
    var c int = len(arr3)
    // Test
    task.findCommon(arr1, arr2, arr3, a, b, c)
}

Output

  1  5  8  9
<?php
// Php Program 
// Find common elements in three unsorted arrays
class SimilarElements
{
    public  function findCommon($arr1, $arr2, $arr3, $a, $b, $c)
    {
        // This are used to collect frequency of array elements
        $record = array();
        $result = false;
        // Collect distinct elements in first array
        for ($i = 0; $i < $a; ++$i)
        {
            if (!array_key_exists($arr1[$i], $record))
            {
                $record[$arr1[$i]] = 1;
            }
        }
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        for ($i = 0; $i < $b; ++$i)
        {
            if (array_key_exists($arr2[$i], $record) && $record[$arr2[$i]] == 1)
            {
                $record[$arr2[$i]] = 2;
            }
        }
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        for ($i = 0; $i < $c; ++$i)
        {
            if (array_key_exists($arr3[$i], $record) && $record[$arr3[$i]] == 2)
            {
                $record[$arr3[$i]] = 3;
            }
        }
        // Display all common elements
        foreach($record as $key => $value)
        {
            if ($value == 3)
            {
                $result = true;
                echo("  ".$key);
            }
        }
        if ($result == false)
        {
            echo(" None ");
        }
    }
}

function main()
{
    $task = new SimilarElements();
    $arr1 = array(4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1);
    $arr2 = array(8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6);
    $arr3 = array(5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6);
    // Get the size
    $a = count($arr1);
    $b = count($arr2);
    $c = count($arr3);
    // Test
    $task->findCommon($arr1, $arr2, $arr3, $a, $b, $c);
}
main();

Output

  1  8  9  5
// Node JS Program 
// Find common elements in three unsorted arrays
class SimilarElements
{
    findCommon(arr1, arr2, arr3, a, b, c)
    {
        // This are used to collect frequency of array elements
        var record = new Map();
        var result = false;
        // Collect distinct elements in first array
        for (var i = 0; i < a; ++i)
        {
            if (!record.has(arr1[i]))
            {
                record.set(arr1[i], 1);
            }
        }
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        for (var i = 0; i < b; ++i)
        {
            if (record.has(arr2[i]) && record.get(arr2[i]) == 1)
            {
                record.set(arr2[i], 2);
            }
        }
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        for (var i = 0; i < c; ++i)
        {
            if (record.has(arr3[i]) && record.get(arr3[i]) == 2)
            {
                record.set(arr3[i], 3);
            }
        }
        // Display all common elements
        for (let [key, value] of record)
        {
            if (value == 3)
            {
                result = true;
                process.stdout.write("  " + key);
            }
        }
        if (result == false)
        {
            process.stdout.write(" None ");
        }
    }
}

function main()
{
    var task = new SimilarElements();
    var arr1 = [4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1];
    var arr2 = [8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6];
    var arr3 = [5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6];
    // Get the size
    var a = arr1.length;
    var b = arr2.length;
    var c = arr3.length;
    // Test
    task.findCommon(arr1, arr2, arr3, a, b, c);
}
main();

Output

  1  8  9  5
#  Python 3 Program 
#  Find common elements in three unsorted arrays
class SimilarElements :
    def findCommon(self, arr1, arr2, arr3, a, b, c) :
        #  This are used to collect frequency of list elements
        record = dict()
        result = False
        i = 0
        #  Collect distinct elements in first list
        while (i < a) :
            if (not(arr1[i] in record.keys())) :
                record[arr1[i]] = 1
            
            i += 1
        
        i = 0
        #  Set the element frequency 2 that is present in the 
        #  first and second lists
        while (i < b) :
            if ((arr2[i] in record.keys()) and record.get(arr2[i]) == 1) :
                record[arr2[i]] = 2
            
            i += 1
        
        i = 0
        #  Set the element frequency 3 that is present in the 
        #  first second and third lists
        while (i < c) :
            if ((arr3[i] in record.keys()) and record.get(arr3[i]) == 2) :
                record[arr3[i]] = 3
            
            i += 1
        
        for key, value in record.items() :
            if (value == 3) :
                result = True
                print("  ", key, end = "")
            
        
        if (result == False) :
            print(" None ", end = "")
        
    

def main() :
    task = SimilarElements()
    arr1 = [4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1]
    arr2 = [8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6]
    arr3 = [5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6]
    #  Get the size
    a = len(arr1)
    b = len(arr2)
    c = len(arr3)
    #  Test
    task.findCommon(arr1, arr2, arr3, a, b, c)

if __name__ == "__main__": main()

Output

   1   5   8   9
#  Ruby Program 
#  Find common elements in three unsorted arrays
class SimilarElements 
    def findCommon(arr1, arr2, arr3, a, b, c) 
        #  This are used to collect frequency of array elements
        record = Hash.new()
        result = false
        i = 0
        #  Collect distinct elements in first array
        while (i < a) 
            if (!record.key?(arr1[i])) 
                record[arr1[i]] = 1
            end
            i += 1
        end

        i = 0
        #  Set the element frequency 2 that is present in the 
        #  first and second arrays
        while (i < b) 
            if (record.key?(arr2[i]) && record[arr2[i]] == 1) 
                record[arr2[i]] = 2
            end
            i += 1
        end

        i = 0
        #  Set the element frequency 3 that is present in the 
        #  first second and third arrays
        while (i < c) 
            if (record.key?(arr3[i]) && record[arr3[i]] == 2) 
                record[arr3[i]] = 3
            end

            i += 1
        end

        #  Display all common elements
        record.each { | key, value |
            if (value == 3) 
                result = true
                print("  ", key)
            end

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

    end

end

def main() 
    task = SimilarElements.new()
    arr1 = [4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1]
    arr2 = [8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6]
    arr3 = [5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6]
    #  Get the size
    a = arr1.length
    b = arr2.length
    c = arr3.length
    #  Test
    task.findCommon(arr1, arr2, arr3, a, b, c)
end

main()

Output

  1  8  9  5
import scala.collection.mutable._;
// Scala Program 
// Find common elements in three unsorted arrays
class SimilarElements()
{
    def findCommon(arr1: Array[Int], arr2: Array[Int], 
      arr3: Array[Int], a: Int, b: Int, c: Int): Unit = {
        // This are used to collect frequency of array elements
        var record: HashMap[Int, Int] = new HashMap[Int, Int]();
        var result: Boolean = false;
        var i: Int = 0;
        // Collect distinct elements in first array
        while (i < a)
        {
            if (!record.contains(arr1(i)))
            {
                record.addOne(arr1(i), 1);
            }
            i += 1;
        }
        i = 0;
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        while (i < b)
        {
            if (record.contains(arr2(i)) && record.get(arr2(i)).get == 1)
            {
                record.addOne(arr2(i), 2);
            }
            i += 1;
        }
        i = 0;
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        while (i < c)
        {
            if (record.contains(arr3(i)) && record.get(arr3(i)).get == 2)
            {
                record.addOne(arr3(i), 3);
            }
            i += 1;
        }
        // Display all common elements
        for ((key, value) <- record)
        {
            if (value == 3)
            {
                result = true;
                print("  " + key);
            }
        }
        if (result == false)
        {
            print(" None ");
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: SimilarElements = new SimilarElements();
        var arr1: Array[Int] = Array(4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1);
        var arr2: Array[Int] = Array(8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6);
        var arr3: Array[Int] = Array(5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6);
        // Get the size
        var a: Int = arr1.length;
        var b: Int = arr2.length;
        var c: Int = arr3.length;
        // Test
        task.findCommon(arr1, arr2, arr3, a, b, c);
    }
}

Output

  1  5  8  9
import Foundation;
// Swift 4 Program 
// Find common elements in three unsorted arrays
class SimilarElements
{
    func findCommon(_ arr1: [Int], _ arr2: [Int], 
      _ arr3: [Int], _ a: Int, _ b: Int, _ c: Int)
    {
        // This are used to collect frequency of array elements
        var record = [Int : Int]();
        var result: Bool = false;
        var i: Int = 0;
        // Collect distinct elements in first array
        while (i < a)
        {
            if (!record.keys.contains(arr1[i]))
            {
                record[arr1[i]] = 1;
            }
            i += 1;
        }
        i = 0;
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        while (i < b)
        {
            if (record.keys.contains(arr2[i]) && record[arr2[i]] == 1)
            {
                record[arr2[i]] = 2;
            }
            i += 1;
        }
        i = 0;
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        while (i < c)
        {
            if (record.keys.contains(arr3[i]) && record[arr3[i]] == 2)
            {
                record[arr3[i]] = 3;
            }
            i += 1;
        }
        // Display all common elements
        for (key, value) in record
        {
            if (value == 3)
            {
                result = true;
                print("  ", key, terminator: "");
            }
        }
        if (result == false)
        {
            print(" None ", terminator: "");
        }
    }
}
func main()
{
    let task: SimilarElements = SimilarElements();
    let arr1: [Int] = [4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1];
    let arr2: [Int] = [8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6];
    let arr3: [Int] = [5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6];
    // Get the size
    let a: Int = arr1.count;
    let b: Int = arr2.count;
    let c: Int = arr3.count;
    // Test
    task.findCommon(arr1, arr2, arr3, a, b, c);
}
main();

Output

   9   5   8   1
// Kotlin Program 
// Find common elements in three unsorted arrays
class SimilarElements
{
    fun findCommon(arr1: Array < Int > , 
                    arr2: Array < Int > , arr3: Array < Int > , 
                    a: Int, b: Int, c: Int): Unit
    {
        // This are used to collect frequency of array elements
        val record: HashMap < Int, Int > = hashMapOf < Int, Int > ();
        var result: Boolean = false;
        var i: Int = 0;
        // Collect distinct elements in first array
        while (i < a)
        {
            if (!record.containsKey(arr1[i]))
            {
                record.put(arr1[i], 1);
            }
            i += 1;
        }
        i = 0;
        // Set the element frequency 2 that is present in the 
        // first and second arrays
        while (i < b)
        {
            if (record.containsKey(arr2[i]) && 
                record.getValue(arr2[i]) == 1)
            {
                record.put(arr2[i], 2);
            }
            i += 1;
        }
        i = 0;
        // Set the element frequency 3 that is present in the 
        // first second and third arrays
        while (i < c)
        {
            if (record.containsKey(arr3[i]) && 
                record.getValue(arr3[i]) == 2)
            {
                record.put(arr3[i], 3);
            }
            i += 1;
        }
        // Display all common elements
        for ((key, value) in record)
        {
            if (value == 3)
            {
                result = true;
                print("  " + key);
            }
        }
        if (result == false)
        {
            print(" None ");
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: SimilarElements = SimilarElements();
    val arr1: Array < Int > = arrayOf(4, 1, 3, 7, 8, 2, 3, 9, 5, -1, -1, -1);
    val arr2: Array < Int > = arrayOf(8, 5, 1, 6, 8, 3, 9, 6, 3, 5, 6);
    val arr3: Array < Int > = arrayOf(5, 7, 2, 4, 7, 9, 7, 8, 1, 1, 8, 6, 6, 6);
    // Get the size
    val a: Int = arr1.count();
    val b: Int = arr2.count();
    val c: Int = arr3.count();
    // Test
    task.findCommon(arr1, arr2, arr3, a, b, c);
}

Output

  1  5  8  9
Python common element in three list


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







© 2021, kalkicode.com, All rights reserved