Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4622926
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:54:45+00:00 2026-05-22T02:54:45+00:00

i was wondering if somebody could help me on my diff function. its supposed

  • 0

i was wondering if somebody could help me on my diff function. its supposed to find the diff between 2 inputs. the function takes in 2 arrays so if array 1 has.. “word1”, “word2” and array 2 has “word1”, “word2”, and “word3”, it should just return “word3”. it works for the most part, but with the certain input below, it doesn’t work.

function diff(a1, a2) {

if (a1.length > a2.length
       || a1.length == a2.length) {
    return [""];
}
a1 = a1.slice(0);
a2 = a2.slice(0);


for (i = 0; i < a1.length; i++) {
    for (k = 0; k < a2.length; k++) {
        if (a1[i] == a2[k]) {
            a1.splice(i, 1);
            a2.splice(k, 1);
        }
    }
}

for (j = 0; j < a2.length; j++) {
    for (p = 0; p < a1.length; p++) {
        if (a2[j] == a1[p]) {
            a2.splice(p, 1);
            a1.splice(j, 1);
        }
    }
}


a1 = a1.concat(a2);
return a1;
}

var s1 = ["one", "two", "three", "four",];
var s2 = ["one", "two", "three", "four", "five"];


document.write(diff(s1, s2));

the answer should just return “five”, but its returning “four”, “four”, “five”. help would be much appreciated. im super stuck. thanks a bunch!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:54:46+00:00Added an answer on May 22, 2026 at 2:54 am

    As far as I understand, you want to return an array containing only differing elements from both. Try it this way, without complex double loops and splices and whatnot:

    function diff(a1, a2) {
    
      var aa1 = {}, aa2 = {}, res = [];
    
      // create an object from a1, containing value:array-index pairs:
      for (var i = 0; i < a1.length; i++) {
          aa1[a1[i]] = i;
      }
      // create an object from a2 like the one from a1
      for (var i = 0; i < a2.length; i++) {
          aa2[a2[i]] = i;
      }
      /**
        * loop through the first object (from a1)
        * if a value in this object is not found in 
        * the object from a2, push that value to 
        * the result array (res)
       */
      for (var l in aa1){
        if (aa1.hasOwnProperty(l) && !(l in aa2)) {
            res.push(a1[aa1[l]]);
        }
      }
      /**
        * but the second array can also contain
        * values differing from the values in the first.
        * So, repeat the above for the object created 
        * from a2
       */
      for (var l in aa2){
       if (aa2.hasOwnProperty(l) && !(l in aa1)) {
            res.push(a2[aa2[l]]);
       }
     }
     /**
       * now res contains all different values
       * note: double values are not counted
       * so ['one','two','tree'] vs ['one','two','four','four']
       * will return ['tree','four']
       * note2: if you want to find the first difference,
       * return res[0]
       */
    
     return res;
    }
    

    If it’s just the first difference you want to find, this (array only) method will do:

    function diff(a1, a2) {
      var firstdiff = null, i = -1;
    
      /**
        * local function to check if value [val] 
        * exists in array [arr]. Called from
        * within the loops
        */
      function check(val,arr){
          var i = -1;
          while(++i<arr.length){
              if(val === arr[i]){ return true;}
          }
          return false;
      }
      // loop a1 and check values vs a2-values
      while (++i<a1.length){
        if (!check(a1[i],a2)) {
          return firstdiff = a1[i];
        }
      }
      // no difference found, continue a2 vs a1
      i = -1;
      while (++i<a2.length){
        if (!check(a2[i],a1)) {
          return firstdiff = a2[i];;
        }
      }
      // there are no differences, firstdiff = null
      return firstdiff;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys just wondering if somebody could help me try and correctly thread my
Was wondering if somebody could please help me solve this. I've follwed all steps
I was wondering if somebody could help me, I can do it on pen
i was wondering if somebody could help me out. I have a plan of
I'm learning Javascript (mainly using JQuery) and was wondering if somebody could help me.
I was wondering if somebody could explain me how pointers and string parsing works.
I never seen this but I wondering if somebody has come across. Having a
I was wondering if somebody could explain to me how to GIT merge master
I'm just wondering how I might do this and hoping that somebody has done
I was wondering if somebody could tell me how the game and the game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.