0

I have one requirement that will get json array object it looks like below(input) json i need to get dupliacte records & unquie records and load it into respective list. condition to check wether array has duplicates or not :: id

let uniqueList = [];
let dupList = [];

Array.prototype.contains = function (item) {
    let filtered_item = this.filter((i) => {
        return i.id === item.id
    });
    return !!filtered_item.length;
}

function contains(list, item) {
    let filtered_item = list.filter((i) => {
        return i.id === item.id
    });
    return !!filtered_item.length;
}

function pushToUniqueList(item) {
    uniqueList.push(item);
}

function pushToDuplicateList(item) {
    dupList.push(item);
}

for (let i = 0; i < objList.length; i++) {
    if (uniqueList.contains(objList[i])) {
        pushToDuplicateList(objList[i]);
    } else {
        pushToUniqueList(objList[i]);
    }
}
Input : [{"obj":{"id":"1234","table":"123"}},{"obj":{"id":"1234","table":"123"}},{"obj":{"id":"12344","table":"123"}}]
output : uniqueList =[{ "obj":{ "id":"12344", "table":"123" }]
dupList =[{"obj":{"id":"1234","table":"123"}},{"obj":{"id":"1234","table":"123"}}]

I have tried with above code but its not working Please help!Thanks in advance!

2
  • Can you add to your question what is the input and what is the current output as well as expected output? Commented Jun 18, 2021 at 17:39
  • Input [{ "obj":{ "id":"1234", "table":"123" } }, { "obj":{ "id":"1234", "table":"123" } }, { "obj":{ "id":"12344", "table":"123" } }] output : uniqueList =[{ "obj":{ "id":"12344", "table":"123" }] dupList =[{ "obj":{ "id":"1234", "table":"123" } }, { "obj":{ "id":"1234", "table":"123" } }] @Molda Commented Jun 18, 2021 at 18:01

1 Answer 1

2

You can try the below, this code runs in linear time

const data = [
      { obj: { id: "1234", table: "123" } },
      { obj: { id: "1234", table: "123" } },
      { obj: { id: "12344", table: "123" } },
    ];
    
    const map = new Map();
    
    for (const entry of data) {
      if (map.has(entry.obj.id)) {
        map.get(entry.obj.id).push(entry);
      } else {
        map.set(entry.obj.id, [entry]);
      }
    }
    
    const uniques = [];
    const duplicates = [];
    
    for (const [id, values] of map.entries()) {
      if (values.length > 1) {
        duplicates.push(...values);
      } else {
        uniques.push(...values);
      }
    }
    
    console.log(`duplicates: ${JSON.stringify(duplicates)}`);
    console.log(`unique: ${JSON.stringify(uniques)}`);

Output

[
  { obj: { id: '1234', table: '123' } },
  { obj: { id: '1234', table: '123' } }
] [ { obj: { id: '12344', table: '123' } } ]
Sign up to request clarification or add additional context in comments.

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.