Mark As Completed Discussion

Good morning! Here's our prompt for today.

Given an array, return another array with just the ordered unique elements from the given array. In other words, you're removing any duplicates.

Note: Order needs to be preserved, so no sorting should be done. And the order should be maintained with the first occurrence of the element in the given array.

Description
JAVASCRIPT
1function uniques(arr) {
2  // fill in
3}
4
5let arr = [3, 5, 6, 9, 9, 4, 3, 12]
6uniques(arr);
7// Correct: [3, 5, 6, 9, 4, 12]
8// But this is incorrect: [5, 6, 9, 4, 3, 12]
9
10arr = [13, 5, 3, 5, 8, 13, 14, 5, 9]
11uniques(arr);
12// Correct: [13, 5, 3, 8, 14, 9]
13// Incorrect: [3, 5, 8, 13, 14, 9]

Constraints

  • Length of the array <= 100000
  • The values in the array between -1000000000 and 1000000000
  • Expected time complexity: O(n)
  • Expected space complexity: O(n)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's a video of us explaining the solution.

To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.

How do I use this guide?

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.