1

Given

[1,3,1,6,22] ,

it should return

[3,6,22]

So far I have this:

const returnUniques = (array) {
  if (array.length == 1) return array[0]
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array.length; j++) {
      if (i !== j) {
        if (array[i] === array[j]) {
        }
      }
    }
  }
  return array
}

I tried

const returnUniques = (array) {
  if (array.length == 1) return array[0]
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array.length; j++) {
      if (i !== j) {
        if (array[i] === array[j]) {
          array.splice(i, 1)
        }
      }
    }
  }
  return array
}

But it just removes duplicate and I could have this by one line with either new Set() or other techniques

So I added

const returnUniques = (array) => {
        if (array.length == 1) return array[0]
        
        for (let i = 0; i < array.length; i++) {
            for (let j = 0; j < array.length; j++) {
                if (i !== j) {
                    if(array[i] === array[j]) {
                       array.splice(i,1)
                       array.splice(j-1, 1)
                    } 
                }
            }
        }
        
        return array
    }

console.log(returnUniques([1,3,1,6,22]))

because I thought that if I removed array[i] so array[j] would be at index of itself -1. But this method doesn't work if there is more than twice the same number and if many other reasons..

Maybe this nested loop isn't the best way to go ? Thanks for helps !

3
  • Your approach is not that bad. However I would create a new empty array in your returnUniques function, fill it with the entries that pass your test and then return the new array. Don't modify the original array. Commented Nov 7, 2020 at 14:21
  • But how wil I fill the newArray with values that doesn't have duplicates ? Commented Nov 7, 2020 at 14:23
  • 1
    First I tried to change if (array[i] == array[j]) to (array[i] !== array[j]) but that doesn't do the trick lol. Thanks for you indications I'm gonna find the solution by trying more :) Commented Nov 7, 2020 at 14:28

6 Answers 6

4

Using a Set and filter to check if first and last index are the same

const returnUniques = (arr) => {
   return [...new Set(arr)].filter(n => arr.indexOf(n) === arr.lastIndexOf(n))    
}

console.log(returnUniques([1,3,1,6,22]))

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

5 Comments

No I need to return [3,6,22]
You didn't read that I said that I could have used set method ?
Thanks, with the filter it's better, but why are we comparing the first index and last index only ? I mean, how your filter work in this case ?
Oh I see, if arr.indexOf(n) !== arr.lastIndexOf(n) it means that there is more than one like this so don't add it, am I right ?
Yes... for a very large data set this may not be the most efficient but for smaller sets should work fine
2

Using Array.prototype.reduce, you can get the counts for each item.

And based on that content, you can filter the array items which are appeared only one time.

const input = [ 1, 3, 1, 6, 22 ];

const uniques = (arr) => {
  const groupBy = arr.reduce((acc, cur) => {
    acc[cur] ? acc[cur] ++ : acc[cur] = 1;
    return acc;
  }, {});
  return Object.entries(groupBy).filter(([key, count]) => count === 1).map(([key, count]) => Number(key));
};

console.log(uniques(input));

Comments

1

You can do something like this.

const removeDuplicates = (arr) => {
  const results = [];

  for (const v of arr) {
    const occurences = arr.filter((x) => x === v);

    if (occurences.length === 1) results.push(v);
  }

  return results;
};

console.log(removeDuplicates([1, 3, 1, 6, 22]));

1 Comment

Nice. I like this solution the most as it is solid clean code that is easy to understand.
1

if the first index is equal to last index of the element then it is unique:

const unique = (array) =>
array.filter((e,idx,arr) => arr.indexOf(e) === arr.lastIndexOf(e))
console.log(unique([1,3,1,6,22]))

Comments

0

Something like this

let arrayData = [1, 3, 1, 6, 22];

let getUniqueValuesFunc = (arrayParam) => {
  let unqiueValues = [];

  for (let unqiueValue of arrayParam ) {
    let compare = arrayParam.filter((x) => x === unqiueValue );

    if (compare.length === 1) unqiueValues.push(unqiueValue);
  }
  return unqiueValues;
};

console.log(getUniqueValuesFunc( arrayData ));

2 Comments

Love this one ! easily understandable :)
This is just the same as the solution of @Rvfvl above. :-)
0

Solution in single loop:

const input = [1, 3, 1, 1, 6, 22];

const returnUniques = (array) => {
    let output = [];
    let filter = [];
    for (let j = 0; j < array.length; j++) {
        const index = output.indexOf(array[j])
        if (index==-1) {
            output.push(array[j])
        } else {
            filter.push(array[j]);
        }
    }
    output = output.filter((o)=>(filter.indexOf(o)==-1))
    return output;
}

console.log(returnUniques(input));

2 Comments

This won't work if for example 1 is within input three times.
@AbishekKumar Good reflection though !

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.