1

There is names array includes the name object.

I tried to use this way, but I think there is a better way to solve.

const names = [
  {name: 'aa'},
  {name: 'bb'},
  {name: 'cc'},
  {name: 'aa'}
]

const isDuplicatedName = names.some(({name}, index, names) => {
  return names.filter((row, idx) => row.name === name && index !== idx).length;
})

console.log(isDuplicatedName)

Is there another better way to solve this using some and filter?

6 Answers 6

2

You could take advantage of sets as they can only contain unique values, make a new set from the vales and check the .size() of the set against the original array length.

Little shorter, but would recommend sticking with something you understand that maintains readability.

const names = [
  {name: 'aa'},
  {name: 'bb'},
  {name: 'cc'},
  {name: 'aa'}
]

const isDuplicatedName = new Set(names.map(a => a.name)).size !== names.length;


console.log(isDuplicatedName)

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

2 Comments

you can also use new Set(names.map(a => a.name)).size
Ahh I see what you mean, edited in, thanks for pointing out! dont work with sets all to much
2

On each iteration, check to see if the current name being iterated over is included in the set (and if so, break), then add the current name to that Set, for an overall complexity of O(n):

const names = [
  {name: 'aa'},
  {name: 'bb'},
  {name: 'cc'},
  {name: 'aa'}
];

const namesSet = new Set();

const isDuplicatedName = names.some(({name}) => {
  if (namesSet.has(name)) {
    return true;
  }
  namesSet.add(name);
  return false;
});

console.log(isDuplicatedName)

(in contrast, .filter inside .some has O(n^2) complexity)

Looking at your original code, for a general rule, if you ever see code like arr.filter(callback).length;, you can improve it by not constructing an unnecessary intermediate array by using reduce instead:

return names.filter((row, idx) => row.name === name && index !== idx).length;

can be changed to

return names.reduce((countSoFar, row, idx) => countSoFar + (row.name === name && index !== idx), 0);

(which would probably be more appropriate, but would still be more inefficient than the Set solution)

1 Comment

Tbh I like yours answer more than mine.
1

if possible, you can .map the array so that you have only an array with names. Then you can use .includes to find any matches.

const names = [
  {name: 'aa'},
  {name: 'bb'},
  {name: 'cc'},
  {name: 'aa'}
]

const isDuplicatedName = names.map(o => o.name).some((name, index, names) => names.includes(name))

console.log(isDuplicatedName)

Comments

1

This should work with only one loop:

    function checkIfArrayIsUnique(arr) {
    var map = {}, i, size;

    for (i = 0, size = arr.length; i < size; i++){
        if (map[arr[i]]){
            return false;
        }

        map[arr[i]] = true;
    }

    return true;
}

Comments

1

reduce the data to a map of key: name, value: count. Then take the keys of the map via Object.keys and check whether a single value is > 1 via some. The complexity of this is O(n).

const names = [{name:'aa'},{name:'bb'},{name:'cc'},{name:'aa'}]

function isDuplicatedName (arr) {
  let map = arr.reduce((a,c) => (a[c.name] = (a[c.name] || 0) + 1, a), {});
  return Object.keys(map).some(k => map[k] > 1);
}

console.log(isDuplicatedName(names))

Comments

1

You can also done it by plain JS

function checkDuplicate(array) {
    let obj = {};
    let isExist = false;
    for(name of array){
        console.log(name);
        if(obj[name.name]) {
            isExist = true;
            break;
        } else {
            obj[name.name] = name
        }
    }
    return isExist;
}

Or you can just check the difference between length it is the simplest solution I believe, you must try this, If it really helps you out

let newNames = Array.from(new Set(names));
console.log(newNames.length===names.length)

1 Comment

second one just only work for simple arrays not for array of objects

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.