alboig

ArraySearch - JavascriptModule0

Apr 14th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Array Search
  2. // Given an array of integers, some elements appear twice and others appear once. Each integer is in the range of 1, N,
  3. // where N is the number of elements in the array.
  4. // Find all the integers of 1, N inclusive that do NOT appear in this array.
  5. // Input
  6. // Read from the standard input:
  7. // There is one line of input, containing N amount of integers, seperated by a comma (",")
  8. // Output
  9. // Print to the standard output:
  10. // There is one line of output, containing the sorted integers, seperated by a comma (",")
  11. // Constraints
  12. // N will always be in the range of 5, 1000
  13. // Sample Tests
  14. // Input
  15. // 1,2,3,3,5
  16. // Output
  17. // 4
  18. // Input
  19. // 4,3,2,7,8,2,3,1
  20. // Output
  21. // 5,6
  22. // Input
  23. // 1,1,1,1,1,1,1,1
  24. // Output
  25. // 2,3,4,5,6,7,8
  26. // CREDIT TO: https://codereview.stackexchange.com/questions/220516/find-all-numbers-disappeared-in-an-array
  27. const input = ['3,1,1,5,1,1,1,5,1'];
  28. const print = this.print || console.log;
  29. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  30. const arrOfItems = gets().split(',').map(Number);
  31. function findMissing (arr) {
  32.   const set = new Set(arr);
  33.   const result = [];
  34.   let i = arr.length;
  35.   while (i) {
  36.     set.has(i--) || result.push(i + 1);
  37.   }
  38.   return result.sort((a, b) => a - b);
  39. }
  40. print(findMissing(arrOfItems).join(','));
Advertisement
Add Comment
Please, Sign In to add comment