最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Finding common minimum value between two arrays - Stack Overflow

programmeradmin2浏览0评论

I am working on a problem in Javascript. Finding common minimum value between two arrays. However, I have been told that this might not work on some values. What is the issue?

function cmp(a, b) { return a - b; }

function findMinimum(A, B) {
    var n = A.length;
    var m = B.length;
    A.sort(cmp);
    B.sort(cmp);
    var i = 0;
    for (var k = 0; k < n; k++) {
        if (i < m - 1 && B[i] < A[k])
            i += 1;
        if (A[k] == B[i])
            return A[k];
    }
    return -1;
}

I am working on a problem in Javascript. Finding common minimum value between two arrays. However, I have been told that this might not work on some values. What is the issue?

function cmp(a, b) { return a - b; }

function findMinimum(A, B) {
    var n = A.length;
    var m = B.length;
    A.sort(cmp);
    B.sort(cmp);
    var i = 0;
    for (var k = 0; k < n; k++) {
        if (i < m - 1 && B[i] < A[k])
            i += 1;
        if (A[k] == B[i])
            return A[k];
    }
    return -1;
}
Share Improve this question edited Aug 5, 2015 at 9:35 Vintage asked Aug 5, 2015 at 9:33 VintageVintage 2381 gold badge2 silver badges13 bronze badges 3
  • Because I need to return the minimum number which is to be found in both arrays. If A[0] and B[0] are different, that will not help. – Vintage Commented Aug 5, 2015 at 9:38
  • try to consider this answer stackoverflow.com/questions/31828623/… – Neil Villareal Commented Aug 5, 2015 at 9:53
  • All you just need to do is merge, sort then get the first index – Neil Villareal Commented Aug 5, 2015 at 9:54
Add a comment  | 

4 Answers 4

Reset to default 7

This should work. Just replace the first if with a while. The while loop loops through array B till it finds an element which is greater than the minimum element of A. Then the outer for loop loops through A to find any element that matches the current element of B or till it reaches an element that is greater than the current element of B, where the process repeats.

function cmp(a, b) {
  return a - b;
}

function findMinimum(A, B) {
  var n = A.length;
  var m = B.length;
  A.sort(cmp);
  B.sort(cmp);
  var i = 0;
  for (var k = 0; k < n; k++) {
    while (i < m - 1 && B[i] < A[k])
      i += 1;
    if (A[k] == B[i])
      return A[k];
  }
  return -1;
}

findMinimum([1,3,5,7], [0,0,1,4,9]);  // 1
findMinimum([3,5,7,9], [1,2,4,7,10]); // 7

Let's take,

A = [ 1, 3, 5, 7]
B = [ 0, 0, 1, 4, 6]

and run through your loop.

Your script fails.

The correct logic should be, you either increment i or k in 1 iteration. Not both

I would do something like,

for (var k = 0; k < n;) {
    if (A[k] == B[i])
        return A[k];

    if (i < m - 1 && B[i] < A[k])
        i += 1;
    else
        k += 1;
}

I'd suggest changing your methodology here. Sorting both of the arrays at the beginning is expensive. Find the intersection set of two arrays and then sort it and return its mimimum value, that's all.

// java code for this 
import java.util.Arrays;

public class CommonMinValue {

public static void main(String[] args) {
    int [] A = {1,5,6,7,8,9,11};
    int [] B = {11,51,16,7,18,19,161};
    
    int n = A.length;
    int m = B.length;;
    Arrays.sort(A);
    System.out.println(Arrays.toString(A));
    System.out.println(Arrays.toString(B));
    Arrays.sort(B);
    int commMin=-1;
    
    int i = 0;
    for (int k = 0; k < n;) {
         if (A[k] == B[i]) {
            commMin = A[k];
            break;
         }
        if (i < m - 1 && B[i] < A[k])
            i ++;
        else
            k ++;
       
    }
   System.out.println("Common minimum value "+commMin);
   
}

}

发布评论

评论列表(0)

  1. 暂无评论