GitHub Repository Submission

When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more

Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.

Exercise 3

Let's now try another pointer based problem, "Compress to Distinct." Let's again start by solving it with a brute-force solution.

// Given a sorted array of integers, your task is to implement
// a function `compressToDistinct` that modifies the array
// in-place to ensure it starts with a sequence of distinct
// elements in their original order. After making these
// modifications, the function should return the count of
// these distinct elements.

// The elements in the latter part of the array, after the
// distinct ones, are not important.

// Example:

// If the input array is [3, 3, 5, 7, 7, 8], there are four
// distinct elements: 3, 5, 7, and 8. After modifying the array
// to place these distinct elements at the beginning, the
// resulting array should look like this -> [3, 5, 7, 8, _, _].
// The underscores (_) represent the elements that are no
// longer important.

// You should name the function `compressToDistinct` for the
// tests to work correctly.

function testCompressToDistinct(array, expectedLength) {
  const originalReference = array;
  const resultLength = compressToDistinct(array);
  const isSameObject = originalReference === array;
  const isLengthCorrect = resultLength === expectedLength;
  const isModifiedCorrectly = array
    .slice(0, expectedLength)
    .every((val, idx, arr) => idx === 0 || val > arr[idx - 1]);

  return isSameObject && isLengthCorrect && isModifiedCorrectly;
}

console.log(testCompressToDistinct([3, 3, 5, 7, 7, 8], 4));
console.log(testCompressToDistinct([1, 1, 2, 2, 2, 3, 4, 4, 5], 5));
console.log(testCompressToDistinct([0], 1));
console.log(testCompressToDistinct([-5, -3, -3, -1, 0, 0, 0, 1], 5));
console.log(testCompressToDistinct([6, 6, 6, 6, 6, 6, 6], 1));

// All tests should log true.

Solution

Here's our brute-force solution:

function compressToDistinct(arr) {
  if (arr.length === 0) return 0;

  const distinct = [];

  for (let i = 0; i < arr.length; i++) {
    if (i === 0 || arr[i] !== arr[i - 1]) {
      distinct.push(arr[i]);
    }
  }

  // Copy back to original array
  for (let i = 0; i < distinct.length; i++) {
    arr[i] = distinct[i];
  }

  return distinct.length;
}

Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...

Submit your solution for LSBot review.

Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...