Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. /*
  2. This algorithm challenge came up in Colt Steele's "JavaScript Algorithms and Data Structures" course:
  3.  
  4. Implement a function called `countUniqueValues` which accepts a sorted array and counts the unique values in the array.
  5. There can be negative numbers in the array, but it will always be sorted.
  6.  
  7. Example output:
  8. ```
  9. countUniqueValues([1, 1, 1, 1, 1, 2]); // 2
  10. countUniqueValues([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13]); // 7
  11. countUniqueValues([]); // 0
  12. countUniqueValues([-2, -1, -1, 0, 1]); // 4
  13. ```
  14.  
  15. So far, he's taught us a couple of techniques that he calls the "Frequency Counter" and "Multiple Pointers".
  16. This particular challenge was presented in the Multiple Pointers section, so we were encouraged to solve it that way
  17. but I thought it'd also be useful to compare it to a solution using the Frequency Counter.
  18.  
  19. Below are my three solutions to the problem:
  20. */
  21.  
  22. // 1. Using multiple pointers BEFORE I looked at his hints on how to approach it:
  23.  
  24. function countUniqueValuesWithMultiplePointersImmutable(sortedArr) {
  25. if (!sortedArr.length) {
  26. return 0;
  27. }
  28.  
  29. let left = 0;
  30. let right = sortedArr.length - 1;
  31. let uniqueValues = 1;
  32.  
  33. while (left < right) {
  34. if (sortedArr[left] === sortedArr[right]) {
  35. break;
  36. }
  37. if (left <= sortedArr.length - right) {
  38. if (sortedArr[left] !== sortedArr[left + 1]) {
  39. uniqueValues++;
  40. }
  41. left++;
  42. }
  43. else {
  44. if (sortedArr[right] !== sortedArr[right - 1]) {
  45. uniqueValues++;
  46. }
  47. right--;
  48. }
  49. }
  50. return uniqueValues;
  51. }
  52.  
  53. console.log(countUniqueValuesWithMultiplePointersImmutable([1, 1, 1, 1, 1, 2])); // 2
  54. console.log(countUniqueValuesWithMultiplePointersImmutable([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13])); // 7
  55. console.log(countUniqueValuesWithMultiplePointersImmutable([])); // 0
  56. console.log(countUniqueValuesWithMultiplePointersImmutable([-2, -1, -1, 0, 1])); // 4
  57.  
  58.  
  59. // 2. Using multiple pointers AFTER I looked at his hints on how to approach it.
  60.  
  61. function countUniqueValuesWithMultiplePointersMutable(sortedArr) {
  62. if (!sortedArr.length) {
  63. return 0;
  64. }
  65. if (sortedArr.length === 1) {
  66. return 1;
  67. }
  68.  
  69. let pointer1 = 0;
  70. let pointer2 = 1;
  71. while (pointer2 < sortedArr.length) {
  72. if (sortedArr[pointer1] !== sortedArr[pointer2]) {
  73. pointer1++;
  74. sortedArr[pointer1] = sortedArr[pointer2];
  75. }
  76. pointer2++;
  77. }
  78. return pointer1 + 1;
  79. }
  80.  
  81. console.log(countUniqueValuesWithMultiplePointersMutable([1, 1, 1, 1, 1, 2])); // 2
  82. console.log(countUniqueValuesWithMultiplePointersMutable([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13])); // 7
  83. console.log(countUniqueValuesWithMultiplePointersMutable([])); // 0
  84. console.log(countUniqueValuesWithMultiplePointersMutable([-2, -1, -1, 0, 1])); // 4
  85.  
  86.  
  87. // 3. Using frequency counter
  88.  
  89. function countUniqueValuesWithFrequencyCounter(sortedArr) {
  90. const frequencyCounter = sortedArr.reduce((accum, current) => {
  91. accum[current] = accum[current] + 1 || 1;
  92. return accum;
  93. }, {});
  94. return Object.keys(frequencyCounter).length;
  95. }
  96.  
  97. console.log(countUniqueValuesWithFrequencyCounter([1, 1, 1, 1, 1, 2])); // 2
  98. console.log(countUniqueValuesWithFrequencyCounter([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13])); // 7
  99. console.log(countUniqueValuesWithFrequencyCounter([])); // 0
  100. console.log(countUniqueValuesWithFrequencyCounter([-2, -1, -1, 0, 1])); // 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement