skip to Main Content

I have a JSON array with three objects. How do I determine if any of them are a duplicate object? I do not need to know which one is duplicated, just if it exists.

6

Answers


  1. Regardless of your data structure, I think this will help you to understand how to find that,

    function hasDuplicateObjects(jsonArray) {
      const seenObjects = new Set();
    
      for (let i = 0; i < jsonArray.length; i++) {
        const obj = jsonArray[i];
        const objStr = JSON.stringify(obj);
    
        if (seenObjects.has(objStr)) {
          return true;
        }
    
        seenObjects.add(objStr);
      }
    
      return false;
    }
    
    
    const jsonData = [
      {"name": "John", "age": 25},
      {"name": "Scott", "age": 30},
      {"name": "John", "age": 25}
    ];
    
    if (hasDuplicateObjects(jsonData)) {
      console.log("Duplicate objects exist in the array");
    } else {
      console.log("No duplicate objects found");
    }
    Login or Signup to reply.
  2. You could stringify the objects and then compare:

    let objA = JSON.stringify(jsonArray[0])
    let objB = JSON.stringify(jsonArray[1])
    let objC = JSON.stringify(jsonArray[2])
    
    return objA === objB || objB === objC || objA === objC
    

    This link has additional ways to do a deep comparison of objects: https://www.syncfusion.com/blogs/post/5-different-ways-to-deep-compare-javascript-objects.aspx

    Login or Signup to reply.
  3. You can "hash" each item and populate a Set. If you detect a hit, there is a duplicate.

    const items = [
      { foo: 1, bar: [] },
      { foo: 2, bar: [] },
      { foo: 1, bar: [] }
    ];
    
    const containsDuplicate = (arr, hashFn) => {
      const cache = new Set();
      for (let item of arr) {
        const hash = hashFn ? hashFn(item) : JSON.stringify(item);
        if (cache.has(hash)) {
          return true; // Short-circuit 
        }
        cache.add(hash);
      }
      return false;
    };
    
    console.log(containsDuplicate(items)); // true
    console.log(containsDuplicate(items, ({ foo }) => foo)); // true
    Login or Signup to reply.
  4. You can find duplicates like this
    dup = arr.filter(obj=> dupId.includes(obj.id));

    Login or Signup to reply.
  5. Here, we determine if there are duplicate objects in a JSON array by utilizing JavaScript’s Set object

    function hasDuplicateObjects(jsonArray) {
      const seenObjects = new Set();
      
      for (const obj of jsonArray) {
        const jsonString = JSON.stringify(obj);
        
        if (seenObjects.has(jsonString)) {
          return true; // Duplicate object found
        }
        
        seenObjects.add(jsonString);
      }
      
      return false; // No duplicate objects found
    }
    
    // Usage example:
    const jsonArr = [
      { "id": 1, "name": "John" },
      { "id": 2, "name": "Jane" },
      { "id": 1, "name": "John" } // Duplicate object
    ];
    
    const hasDuplicates = hasDuplicateObjects(jsonArr);
    console.log(hasDuplicates); // Output: true
    
    
    Login or Signup to reply.
  6. If you need speed here, you could manually compare any objects by traversing:

    function isSame(a, b) {
    
        if (Object.is(a, b)) {
            return true;
        }
    
        if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
            for (let i = 0; i < a.length; i++) {
                if (!isSame(a[i], b[i])) {
                    return false;
                }
            }
            return true;
        } else if ((typeof a === 'object') && (typeof b === 'object')) {
            for (const key in a) {
                if (!isSame(a[key], b[key])) {
                    return false;
                }
            }
            return true;
        }
    
        return false;
    
    }
    
    let hasDuplicate = false;
    
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            const [a, b] = [arr[i], arr[j]];
            if (isSame(a, b)) {
                hasDuplicate = true;
                break;
            }
        }
    }
    console.log('the array has a duplicate:', hasDuplicate);
    <script>
    const arr = [{
    
            test: {
                keys: ['some', 'test', 'keys', 'are', 'here']
            },
            values: {
                prop1: true,
                prop2: false
            },
            scalar: 'scalar'
        },
        {
    
            test: {
                keys: ['some', 'test', 'keys', 'are', 'here']
            },
            values: {
                prop1: true,
                prop2: false
            },
            scalar: 'scalar2'
        },
        {
    
            test: {
                keys: ['some', 'test', 'keys', 'are', 'here']
            },
            values: {
                prop1: true,
                prop2: false
            },
            scalar: 'scalar'
        }];
    </script>

    And benchmarking with JSON.stringify:

    enter image description here

    <script benchmark data-count="100000">
        const chunk = [{
    
            test: {
                keys: ['some', 'test', 'keys', 'are', 'here']
            },
            values: {
                prop1: true,
                prop2: false
            },
            scalar: 'scalar'
        },
        {
    
            test: {
                keys: ['some', 'test', 'keys', 'are', 'here']
            },
            values: {
                prop1: true,
                prop2: false
            },
            scalar: 'scalar2'
        },
        {
    
            test: {
                keys: ['some', 'test', 'keys', 'are', 'here']
            },
            values: {
                prop1: true,
                prop2: false
            },
            scalar: 'scalar3'
        },
        ];
    
        const arr = [];
        let count = 2;
        while (count--) {
            arr.push(...chunk.map(item => (item.random = Math.random()+Math.random()) && structuredClone(item)));
        }
    
        // @benchmark JSON.stringify (AlwaysSunny)
    
        function hasDuplicateObjects(jsonArray) {
            const seenObjects = new Set();
    
            for (let i = 0; i < jsonArray.length; i++) {
                const obj = jsonArray[i];
                const objStr = JSON.stringify(obj);
    
                if (seenObjects.has(objStr)) {
                    return true;
                }
    
                seenObjects.add(objStr);
            }
    
            return false;
        }
    
        // @run 
        hasDuplicateObjects(arr);
    
        // @benchmark Alexander
    
        function isSame(a, b) {
    
            if (Object.is(a, b)) {
                return true;
            }
    
            if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
                for (let i = 0; i < a.length; i++) {
                    if (!isSame(a[i], b[i])) {
                        return false;
                    }
                }
                return true;
            } else if ((typeof a === 'object') && (typeof b === 'object')) {
                for (const key in a) {
                    if (!isSame(a[key], b[key])) {
                        return false;
                    }
                }
                return true;
            }
    
            return false;
    
        }
    
        // @run
    
        let hasDuplicate = false;
    
        for (let i = 0; i < arr.length; i++) {
            for (let j = i + 1; j < arr.length; j++) {
                const [a, b] = [arr[i], arr[j]];
                if (isSame(a, b)) {
                    hasDuplicate = true;
                    break;
                }
            }
        }
        hasDuplicate;
    
    
    
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search