Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Array data structure

I strucked at the line 11th line of the code , can we return the code insted of doing that , i am getting confused.

 var findPairs = function(nums, k) {
        let prev = {} , pairs = 0;
        nums.sort((a,b)=>a-b);
        for(var i=0;i<nums.length;i++){
            let key = nums[i];
            if(!prev[key]){
                let diff = key - k;
                if(prev[diff]!==undefined){
                    pairs++;
                }
                prev[key] = 1;
              }else if(k===0){
                  if(prev[key]===1){
                      pairs++
                  }
                  
                  prev[key]++;
              }
            }
        return pairs;
    };
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

I made a lot of assumptions, but if my assumptions are correct you are asking why we need else if statement. I assume that this function searches for the pairs in array that have difference k. If this is correct, then the else if case is needed when k is 0 and in array you have same element multiple times. For example:

If you have k=0 and nums=[3, 4, 5, 3], we have to count 3 and 3 since they are the pairs too.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!