user6248190
user6248190

Reputation: 1271

Find lowest value present in exactly one array

How can I write a function which finds the lowest value which is not 1 and is present in exactly one array?

For example:

const x = [1, 8, 7, 3, 4, 1, 8];
const y = [6, 4, 1, 8, 5, 1, 7];

should return 3 because it is the lowest number which is only in one array(x). if there are no unique values then it should return a number greater than 1 and less than the second smallest number. for instance

const x = [5, 5, 5, 7 ,7 ,7];
const y = [3, 4, 5, 1, 3, 7];

The above should return 2 as there are multiple occurrences of 3 and the lowest unique value is 1 so the number which should be returned is 2.

I have tried the following.

function lowUniwueValue(x, y) {

  const uniqueVal = parseInt(x.filter((obj) => y.indexOf(obj) == -1).toString());

  return uniqueVal;
}

The above function returns the unique value but I cannot guarantee it will be the lowest.

I have also tried creating a new set, which only has unique values but then the problem is we can't see if there are duplicates for the low number.

Upvotes: 1

Views: 86

Answers (3)

Giovanni Esposito
Giovanni Esposito

Reputation: 11156

Ciao, absoultely not comparable to the elegance of the other answers but I tried.

function findMin(x, y) {
   let array = [Math.min(...x.filter(el => el !== 1)), Math.min(...y.filter(el => el !== 1))];          
   if (array[0] !== array[1]) {
  let occx = x.reduce((a, v) => (v === array[0] ? a + 1 : a), 0);
  let occy = y.reduce((a, v) => (v === array[1] ? a + 1 : a), 0);
  if (occx === 1 && occy === 1) return Math.min(...array);
  else {
     let min = Math.min(...array) - 1;
     if (min !== 1) return min;
     else {
        // if min value -1 === 1 remove min elements found and continue to find 
        let indexa = x.indexOf(array[0]);
        let indexb = y.indexOf(array[1]);
        x.splice(indexa, 1);
        y.splice(indexb, 1);
        findMin(x, y);
     }
   }
}
else {
   // if min values found are equal, remove them and continue to find
   let indexa = x.indexOf(array[0]);
   let indexb = y.indexOf(array[1]);
   x.splice(indexa, 1);
   y.splice(indexb, 1);
   findMin(x, y);
}
}

console.log(findMin([1, 8, 7, 3, 4, 1, 8], [6, 4, 1, 8, 5, 1, 7]))
console.log(findMin([5, 5, 5, 7 ,7 ,7], [3, 4, 5, 1, 3, 7]))

I found min of both arrays, then if they are different, return the minimum value. Else, count occurrencies of min values found in both arrays and in case there are more than 1 occurrencies, reduce by 1 the result and, if it's different from 1, return it else continue to find min value (removing previuos elements found).

Upvotes: 0

Unmitigated
Unmitigated

Reputation: 89139

You can find the frequency of each number and also find the minimum number greater than one (from both arrays). If its frequency is exactly one, it is the answer; otherwise, the answer is two.

const getMin = (arr, arr2) => {
  let freq = {},
    min = Infinity;
  [...arr, ...arr2].forEach(x => {
    freq[x] = (freq[x] || 0) + 1;
    if (x > 1 && x < min) min = x;
  });
  return freq[min] === 1 ? min : 2;
};
console.log(getMin([1, 8, 7, 3, 4, 1, 8], [6, 4, 1, 8, 5, 1, 7]));
console.log(getMin([5, 5, 5, 7, 7, 7], [3, 4, 5, 1, 3, 7]));

Upvotes: 2

Yash Shah
Yash Shah

Reputation: 1654

Use Array Indexing:

const x = [1, 8, 7, 3, 4, 1, 8];

const y = [6, 4, 1, 8, 5, 1, 7];

Step 1: Iterate over array x and mark that element as visited by marking the index of Indexing array as 1.

Step 2: Iterate over array y and mark that element as visited by marking the index of Indexing array as 1 which has initial value 0 and 2 which has an initial value 1.

Step 3: Indexing array becomes arr = [0,2,0,1,2,1,1,2,2]

The answer will be the smallest value which is an index marked 1 and not equal to 1 i.e. 3.

Upvotes: 0

Related Questions