AnujKosambi

swmansion_algorithmic_task

Sep 25th, 2020
1,267
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * after 6 every prime number will be form of 6n-1 and 6n+1
  3.  * @param element
  4.  */
  5. function isPrime(element) {
  6.   if (element < 6) {
  7.     if (element < 2) {
  8.       return false;
  9.     }
  10.     return element !== 4;
  11.   }
  12.   return (element + 1) % 6 === 0 || (element - 1) % 6 === 0
  13. }
  14.  
  15. function countFrequencies(items) {
  16.   return items.reduce((accum, number) => {
  17.     if (!accum[number]) {
  18.       accum[number] = 0;
  19.     }
  20.     accum[number]++;
  21.     return accum;
  22.   }, {});
  23. }
  24.  
  25. function output(A, B) {
  26.   const freq = countFrequencies(B);
  27.   return A.reduce((accum, number) => {
  28.     if (isPrime(freq[number])) {
  29.       return accum;
  30.     }
  31.     return [...accum, number]
  32.   }, [])
  33. }
  34.  
  35. /**
  36.  * time complexity is O(A + B)
  37.  */
  38.  
RAW Paste Data