Unique Values Array Function

Adrachin:

Write a function getUniqueValues(arr) that takes an array of numbers as input and returns a new array containing only the unique values in the input array, in ascending order.

Requirements:

Use ES6 syntax and features, such as arrow functions, spread operator, and Set.

Do not use any external libraries or built-in array methods like filter() or indexOf().

The output array should only contain unique values, i.e., no duplicates.

The output array should be sorted in ascending order.

Example Input:

getUniqueValues([3, 1, 4, 2, 2, 1, 5, 6, 3, 4, 7]);

Example Output:

[1, 2, 3, 4, 5, 6, 7]

Note: The order of the unique values in the output array may vary.

You can implement the solution using ES6 syntax and features, and leverage the Set data structure to efficiently remove duplicates. Remember to sort the output array in ascending order before returning it.

LiquidGPT:

Here’s a function that meets your requirements:

const getUniqueValues = arr => {
  // Create a Set from the input array to remove duplicates
  const uniqueSet = new Set(arr);

  // Convert the Set back to an array
  const uniqueArray = [...uniqueSet];

  // Sort the array in ascending order
  uniqueArray.sort((a, b) => a - b);

  // Return the sorted array of unique values
  return uniqueArray;
}

// Example usage:
const result = getUniqueValues([3, 1, 4, 2, 2, 1, 5, 6, 3, 4, 7]);
console.log(result); // Output: [1, 2, 3, 4, 5, 6, 7]

This function takes an array as input, converts it into a Set to remove duplicates, then converts the Set back into an array and sorts it in ascending order. Finally, it returns the sorted array containing unique values.