0

I'm studing a solution of this lesson:

https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/

I headed up of this solution made my a github user. https://github.com/daraosn/codility/tree/master/02-CountingElements/02-PermCheck/javascript

I did understand everything of the code below:

function solution(A) {
        var N = A.length;
        var sum = (N * (N+1)) / 2;
        var tap = [];
        for (var i in A) {
            sum-=A[i];
            if(tap[A[i]]) {
                return 0;
            }
            tap[A[i]] = true;
        }
        return +(sum==0);
    }

with exception of these code lines below:

if(tap[A[i]]) {
  return 0;
}
tap[A[i]] = true;

What is its purppose? I didn't understand. I did a test deleting these code lines from the answer in the codility interface and it returned 75% right instead of 100% when I had these lines

claudiopb
  • 1,056
  • 1
  • 13
  • 25

3 Answers3

2

function solution(A) {
    const set = new Set(A)
    const max = Math.max(...A)
    return set.size === max && set.size === A.length ? 1:0
}
Sean_X
  • 41
  • 3
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '21 at 11:18
1

That section checks to see if the number being iterated over has been found before, and per the instructions, duplicates are forbidden:

A permutation is a sequence containing each element from 1 to N once, and only once.

On every normal iteration, the current number being iterated over is assigned to a property of tap:

tap[A[i]] = true;

Then, on subsequent iterations, that test checks to see if the new number being iterated over has already been used:

if(tap[A[i]]) {
  return 0;
}

This helps to invalidate inputs like [2, 2, 2], while permitting inputs like [1, 2, 3].

That said, there are two major red flags with this. First, for..in shouldn't be used to iterate over arrays. Instead:

for (const num of A) {
  // use num
}

Also, sparse arrays are a very bad idea - it would make much more sense to use an object:

var tap = {};

or a Set:

var tap = new Set();
for (const num of A) {
  sum -= num;
  if (tap.has(num)) {
    return 0;
  }
  tap.add(num);
}
return +(sum == 0);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Array solution is not so proper way such above explaining. But I will put the solution(O(n)) in case you want :)

const solution = A => ~~(A.sort((a,b) => a-b).every((a,i) => a === i+1));
REN
  • 53
  • 1
  • 6
  • Hi , i really like your answer , i went a very similar route and only got 75% score for the test and I'm struggling to understand why. This was my solution: function solution(A) { A.sort() for(let i = 0 ; i < A.length; i++){ if(A[i] !== i + 1){ return 0 } } return 1 } If you ever get a chance could you explain to me why there's a difference between the two and how does the ~~ applied on the boolean affect the outcome? Thanks in advance – Apophis Mar 24 '21 at 12:47
  • ok i figured the ~~ turns the boolean into an integer. Not sure i understand how though. – Apophis Mar 24 '21 at 12:51
  • 1
    Ok, i figured what my solution was missing was specifying the sorting function, i thought by default it was ascending order, apparently not. So adding A.sort((a,b) => a-b)) resulted in 100%. Anyway i wanna say i really like your solution, it's so elegant, great job – Apophis Mar 24 '21 at 12:54