0

I need to count the no of duplicates in an multidimensional array and give alert if duplicates found.

Arr =[[2,"sk"],[3,"df"],[7,"uz"],[3,"df"],[7,"gh"]]

Suggestions: Count can be done in this manner that if arr[0] position is equals to next coming positions then it must give a count & needs to check both values combination same. And then for arr[1] position to check for next coming positions and so on for other position till last

Only counts the exact combination Like [3,"df"] at second position equal to [3,"df"] at fourth combination

Expected Output Count :1 Alert duplicate data found

1
  • What would the count be for [[2,"sk"],[2,"sk"],[3,"df"],[3,"df"],[3,"df"]]? Commented Jun 13, 2021 at 5:04

4 Answers 4

0

My answer from the original question (except that I've got three items like [3, "df"] in the input array) is:

const input = [
  [2, "sk"], [3, "df"], [7, "uz"], [3, "df"], [7, "gh"],
  [5, "df"], [21, "sk"], [2, "1sk"], [3, "df"]
];

const duplicate_count = input.length - new Set( input.map(JSON.stringify) ).size;

console.log(duplicate_count);

If the count for [[3,"df"],[3,"df"],[3,"df"]] should be one instead of two then perhaps something like:

const input = [
  [2, "sk"], [3, "df"], [7, "uz"], [3, "df"], [7, "gh"],
  [5, "df"], [21, "sk"], [2, "1sk"], [3, "df"]
];

const duplicate_count = [
  ...input
    .map(JSON.stringify)
    .reduce( (acc, v) => acc.set(v, (acc.get(v) || 0) + 1), new Map() )
    .values()
].filter((v) => v > 1).length;

console.log(duplicate_count);

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

Comments

0

You could map to concatenate the numbers and strings, then sort. Finally, reduce the array to the duplicates count.

const input = [
  [2, "sk"],
  [3, "df"],
  [7, "uz"],
  [3, "df"],
  [7, "gh"],
  [7, "df"],
];

const result = input
  .map(([number, string]) => number + string)
  .sort()
  .reduce(
    (acc, cur, i, { [i - 1]: last }) => (cur === last ? acc + 1 : acc),
    0
  );

console.log(`Count: ${result}${result && ' Alert duplicate data found'}`);

Comments

0

    let arr =[[2,"sk"],[3,"df"],[7,"uz"],[3,"df"],[7,"gh"],[2,"sk"],[7,"uz"]]
    
    function getNumDupes(a) {
       return a.length-
    arr.reduce((b,a)=>{if (!b.includes(a.join(""))) b.push(a.join("")); return b;},[]).length 
    }
    
    console.log(getNumDupes(arr) + ' duplicates found');

Comments

0

You can use forEach to iterate the array and then filter and check length if found more than 1, set an entry into Map which holds a key value pair and will keep 1 entry for same key and then can check the size.

let arr =[[2,"sk"],[3,"df"],[7,"uz"],[3,"df"],[7,"gh"]];

let map = new Map();
arr.forEach(e1 => 
                 arr.filter(e2 => e1[0]===e2[0] && e1[1]===e2[1]).length > 1
                 ? map.set(`${e1[0]}-${e1[1]}`,1) 
                 : null
            );

console.log(`${map.size} duplicate found`);

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.