Hone logo
Hone
Problems

Extracting Unique Values from an Array in JavaScript

This challenge focuses on efficiently identifying and extracting unique values from a JavaScript array. Removing duplicates is a common task in data processing and analysis, ensuring you're working with distinct data points. Your goal is to write a function that takes an array as input and returns a new array containing only the unique elements, preserving their original order.

Problem Description

You are tasked with creating a JavaScript function called getUniqueValues that accepts a single argument: an array of any data type (numbers, strings, booleans, or a mix). The function should return a new array containing only the unique values from the input array, maintaining the original order of appearance. The function should not modify the original input array.

Key Requirements:

  • Uniqueness: The output array must contain only unique values. No duplicates are allowed.
  • Order Preservation: The order of the unique elements in the output array must be the same as their first appearance in the input array.
  • New Array: The function must return a new array. It should not modify the original input array.
  • Handles Mixed Data Types: The function should correctly handle arrays containing a mix of data types (e.g., numbers, strings, booleans).
  • Handles Empty Array: The function should gracefully handle an empty input array.

Expected Behavior:

The function should iterate through the input array and add each element to the output array only if it's not already present.

Edge Cases to Consider:

  • Empty input array.
  • Array with all duplicate values.
  • Array with mixed data types.
  • Array with null and undefined values. These should be treated as distinct values.

Examples

Example 1:

Input: [1, 2, 2, 3, 4, 4, 5]
Output: [1, 2, 3, 4, 5]
Explanation: The input array contains duplicate numbers. The output array contains only the unique numbers in their original order.

Example 2:

Input: ["apple", "banana", "apple", "orange", "banana"]
Output: ["apple", "banana", "orange"]
Explanation: The input array contains duplicate strings. The output array contains only the unique strings in their original order.

Example 3:

Input: [1, "1", true, 1, "true", true]
Output: [1, "1", true, "true"]
Explanation:  Different data types are treated as distinct values.  The number `1` is different from the string `"1"`, and the boolean `true` is different from the string `"true"`.

Example 4:

Input: []
Output: []
Explanation: An empty input array results in an empty output array.

Constraints

  • The input array can contain any data type (numbers, strings, booleans, null, undefined, objects, etc.).
  • The length of the input array can be up to 1000 elements.
  • The function should have a time complexity of O(n) or better, where n is the length of the input array. While a nested loop solution will technically work, it will be considered inefficient.
  • The function must be written in standard JavaScript (ES6 or earlier).

Notes

Consider using a Set object to efficiently track unique values. Sets only allow unique elements, making them ideal for this task. Remember to preserve the original order of elements while extracting the unique values. Think about how you can iterate through the input array and add elements to the output array in a way that ensures uniqueness and order preservation.

Loading editor...
javascript