2

I'm trying to achieve to write an array function with the use of reduce and find helpers that returns an array of unique numbers.

var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]

function unique(array) {
  array.reduce((uniqueArray, number) => {
    if (uniqueArray.indexOf(find(array.number))) {
      uniqueArray.push(array.number);
    }
    return uniqueArray;
  }, []);
}
console.log(unique(numbers));
// undefined 
// undefined

When running this code I get

undefined

twice in Browser Javascript console.

9

4 Answers 4

4

You need a return statment.

return array.reduce((uniqueArray // ...
// ^^^

And some better find method with Array.indexOf

function unique(array) {
    return array.reduce((uniqueArray, number) => {
        if (uniqueArray.indexOf(number) === -1) {
            uniqueArray.push(number);
        }
        return uniqueArray;
    }, []);
}

var numbers = [1, 1, 2, 3, 4, 4];
console.log(unique(numbers));

And now with Set and spread syntax ... for collecting the items in a new array.

function unique(array) {
    return [... new Set(array)];
}

var numbers = [1, 1, 2, 3, 4, 4];
console.log(unique(numbers));

Sign up to request clarification or add additional context in comments.

6 Comments

includes is fine, but it does not work in edge, actually.
your find does not work on arrays that contain a 0. You should use some or simply includes.
@Bergi, then indexOf is better, at least as find.
Yes indeed. An alternative (as long as the array does only contain numbers) is ….find(…) != null
That's what I meant by "if the array does only contain numbers" (which undefined is not)
|
3

The reasons for the errors are explained in previous answers. So I just adding an alternate method with Array#filter method.

var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]

function unique(array) {
  return array.filter(function(v, i, arr) {
    // compare index with first element index
    return i == arr.indexOf(v);
  })
}
console.log(unique(numbers));


With ES6 arrow function.

var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]

function unique(array) {
  return array.filter((v, i, arr) => i == arr.indexOf(v))
}
console.log(unique(numbers));


UPDATE : With a reference object instead of checking the index.

var numbers = [1, 1, 2, 3, 4, 4],
  ref = {};


function unique(array) {
  return array.filter(function(v) {
    if (!(v in ref)) {
      ref[v] = true;
      return true;
    }
    return false;
  })
}
console.log(unique(numbers));

Comments

2

You have few errors. First you need to return value from your function and also to check if element is already in uniqueArray you can use indexOf() == -1.

var numbers = [1, 1, 2, 3, 4, 4];

function unique(array) {
  return array.reduce((uniqueArray, number) => {
    if (uniqueArray.indexOf(number) == -1) uniqueArray.push(number)
    return uniqueArray;
  }, []);
}
console.log(unique(numbers));

With ES6/7 you can use includes() and arrow functions like this.

var numbers = [1, 1, 2, 3, 4, 4];

function unique(arr) {
  return arr.reduce((r, n) => (!r.includes(n) ? r.push(n) : 1) && r , []);
}
console.log(unique(numbers));

Comments

0

You can always use Array.includes.

function SillyFunctionName(array) {
    "use strict";

    var uniqueArray = [];

    for (var i = 0; i < array.length; i++) {
        if (uniqueArray.includes(array[i])) {
            break;
        } else {
            uniqueArray.push(array[i]);
        }
    }

    return uniqueArray;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.