Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This algorithm challenge came up in Colt Steele's "JavaScript Algorithms and Data Structures" course:
- Implement a function called `countUniqueValues` which accepts a sorted array and counts the unique values in the array.
- There can be negative numbers in the array, but it will always be sorted.
- Example output:
- ```
- countUniqueValues([1, 1, 1, 1, 1, 2]); // 2
- countUniqueValues([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13]); // 7
- countUniqueValues([]); // 0
- countUniqueValues([-2, -1, -1, 0, 1]); // 4
- ```
- So far, he's taught us a couple of techniques that he calls the "Frequency Counter" and "Multiple Pointers".
- This particular challenge was presented in the Multiple Pointers section, so we were encouraged to solve it that way
- but I thought it'd also be useful to compare it to a solution using the Frequency Counter.
- Below are my three solutions to the problem:
- */
- // 1. Using multiple pointers BEFORE I looked at his hints on how to approach it:
- function countUniqueValuesWithMultiplePointersImmutable(sortedArr) {
- if (!sortedArr.length) {
- return 0;
- }
- let left = 0;
- let right = sortedArr.length - 1;
- let uniqueValues = 1;
- while (left < right) {
- if (sortedArr[left] === sortedArr[right]) {
- break;
- }
- if (left <= sortedArr.length - right) {
- if (sortedArr[left] !== sortedArr[left + 1]) {
- uniqueValues++;
- }
- left++;
- }
- else {
- if (sortedArr[right] !== sortedArr[right - 1]) {
- uniqueValues++;
- }
- right--;
- }
- }
- return uniqueValues;
- }
- console.log(countUniqueValuesWithMultiplePointersImmutable([1, 1, 1, 1, 1, 2])); // 2
- console.log(countUniqueValuesWithMultiplePointersImmutable([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13])); // 7
- console.log(countUniqueValuesWithMultiplePointersImmutable([])); // 0
- console.log(countUniqueValuesWithMultiplePointersImmutable([-2, -1, -1, 0, 1])); // 4
- // 2. Using multiple pointers AFTER I looked at his hints on how to approach it.
- function countUniqueValuesWithMultiplePointersMutable(sortedArr) {
- if (!sortedArr.length) {
- return 0;
- }
- if (sortedArr.length === 1) {
- return 1;
- }
- let pointer1 = 0;
- let pointer2 = 1;
- while (pointer2 < sortedArr.length) {
- if (sortedArr[pointer1] !== sortedArr[pointer2]) {
- pointer1++;
- sortedArr[pointer1] = sortedArr[pointer2];
- }
- pointer2++;
- }
- return pointer1 + 1;
- }
- console.log(countUniqueValuesWithMultiplePointersMutable([1, 1, 1, 1, 1, 2])); // 2
- console.log(countUniqueValuesWithMultiplePointersMutable([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13])); // 7
- console.log(countUniqueValuesWithMultiplePointersMutable([])); // 0
- console.log(countUniqueValuesWithMultiplePointersMutable([-2, -1, -1, 0, 1])); // 4
- // 3. Using frequency counter
- function countUniqueValuesWithFrequencyCounter(sortedArr) {
- const frequencyCounter = sortedArr.reduce((accum, current) => {
- accum[current] = accum[current] + 1 || 1;
- return accum;
- }, {});
- return Object.keys(frequencyCounter).length;
- }
- console.log(countUniqueValuesWithFrequencyCounter([1, 1, 1, 1, 1, 2])); // 2
- console.log(countUniqueValuesWithFrequencyCounter([1, 2, 3, 4, 4, 4, 7, 7, 12, 12, 13])); // 7
- console.log(countUniqueValuesWithFrequencyCounter([])); // 0
- console.log(countUniqueValuesWithFrequencyCounter([-2, -1, -1, 0, 1])); // 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement