Find element from 3 arrays

find common elements in three arrays in c
find common elements in three arrays javascript
find common elements in two arrays in c
how to find common elements in three sorted array in python
intersection of three sorted arrays leetcode
find common elements in k sorted array
find common elements in two unsorted arrays
how find the first repeating element in an array of integers?

I want to find element from 3 arrays and create string using values. I have tried and given the output. But I want to know that, is there any better solution for this.

var numbers = ['1','2','3','4','5'];
var letters = ['A','B','C','D','E'];
var romans = ['I','II','III','IV','V'];

function findInArray(input){
    var index = -1;
    if(numbers.indexOf(input) >= 0){
        index = numbers.indexOf(input);        
    } else if(letters.indexOf(input) >= 0){
        index = letters.indexOf(input);
    } else if(romans.indexOf(input) >= 0){
        index = romans.indexOf(input);
    }

    if(index > -1){
        var data = '{"numbers":"'+numbers[index]+'","letters":"'+letters[index]+'","romans":"'+romans[index]+'"}';
        console.log(data);
    }
}

findInArray('2');

output : {"numbers":"2","letters":"B","romans":"II"}

You don't need to check if indexOf exists for each of the arrays. You can just find the max value of index for all the three arrays.

If the argument exists in any of the array, it will return a positive values (which results in true)

Then you can simply return the concatenation of the result using the template strings

var numbers = ['1','2','3','4','5'];
var letters = ['A','B','C','D','E'];
var romans = ['I','II','III','IV','V'];

var findInArray = (i) => {
  var index = Math.max(numbers.indexOf(i), letters.indexOf(i), romans.indexOf(i));
  if (index) {
    return `{numbers: ${numbers[index]}, letters: ${letters[index]}, romans: ${romans[index]}}`;
  } 
}

console.log(findInArray('2'));
console.log(findInArray('D'));
console.log(findInArray('V'));

Find three element from different three arrays such that a + b + c , We have three sorted arrays and you have to find common numbers which is present in all three arrays. INPUT: ar1[] = {1, 5, 10, 20, 40, 80}. ar2[] = {6, 7, 20, 80​,  Find three closest elements from given three sorted arrays 1) Iterate over all elements of A [], 2) Repeat step 1 for B [] and C []. 3) Return overall minimum.

Vishal,

@Jonas has used a self executing function.

For example it will output 25

(function(x){
  return x*x;
}(5));

here 5is the parameter of this self executing function which will output to 25

Back to answer; When you convert his answer to raw it will look something like

const findInArray = val => (i => ({ //i = 1 here
   numbers: numbers[1],
   letters: letters[1],
   romans: romans[1]
 }))(Math.max(1,-1,-1) //will output 1); 

Hope it makes sense.

resource - http://markdalgleish.com/2011/03/self-executing-anonymous-functions/

Find all common elements in given three sorted arrays, Some improvement in Pelkonen's answer: From converted problem in OP: "Given that there is one and only one common element in these 3  Find the largest three elements in an array. Given an array with all distinct elements, find the largest three elements. Expected time complexity is O(n) and extra space is O(1). Examples : Input: arr[] = {10, 4, 3, 50, 23, 90} Output: 90, 50, 23. Below is algorithm: 1) Initialize the largest three elements as minus infinite.

Might be simpler with:

const findInArray = val => (i => ({
   numbers: numbers[i],
   letters: letters[i],
   romans: romans[i]
 }))(Math.max(
   numbers.indexOf(val), 
   letters.indexOf(val),
   romans.indexOf(val)
));

Find unique common element from 3 arrays, Given three sorted(ascending order) arrays of integers, find out all the common elements in them. Initialize indexOne and indexTwo to the index of smallest element of arrayOne and arrayTwo respectively.(indexOne = indexTwo = 0;) If arrayOne[indexOne] == arrayTwo[indexTwo], we found a common element. Store it in a temporary array and increment both indexOne and indexTwo. If

Find all common numbers in given three sorted arrays., 3. Write a Java program to print the following grid. Go to the editor Write a Java program to find common elements from three sorted (in  Home » Interview » Array Interview Questions » Find all common elements in given three sorted arrays Find all common elements in given three sorted arrays We have three sorted arrays and you have to find common numbers which is present in all three arrays

Java Array exercises: Array Exercises, 3.Write a NumPy program to create a 3x3 matrix with values ranging from Write a NumPy program to find the number of elements of an array,  If you need to find the index of a value, use Array.prototype.indexOf(). (It’s similar to findIndex(), but checks each element for equality with the value instead of using a testing function.) If you need to find if a value exists in an array, use Array.prototype.includes().

NumPy: Array Object - Exercises, Practice, Solution, Given a sorted array, two integers k and x , find the k closest elements to x in the array. The result Input: [1,2,3,4,5], k=4, x=3 Output: [1,2,3,4]. Example 2: The find() method returns the value of the first element in an array that pass a test (provided as a function). The find() method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)

Comments
  • You are searching each array twice.
  • Hint: 1. Don't invoke indexOf twice for each array. 2. What if tomorrow there is one more array to be searched? Would you add another else if?
  • Adding an explanation will help others too !
  • @Jonas please provide explanation
  • Imo.. a self executable function does not make this problem simpler. It's hard to read and not necessary at all. KISS (Keep It Stupid Simple)
  • @Dieterg, It is most convenient answer IMO. Would you please judge your statement ?
  • Write out the self executable function like @Anurag Singh Bisht