Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

469
Views
Javascript:Delete duplicate in 2D Array

I have a 2D array like this one in JavaScript:

result=[["user1", "java"], ["user2", "python"],["user1", "java"], ["user1", "C++"], ["user1", "java"], ["user2", "Python"]....]  

you can see that ["user1", "java"] comes 3 times and ["user2", "python"] 2 times.

I want to clean this array so that every couple of element must appear only once.it means that if "user1" can appear again, it should be with another language but not with "java". can some one help me?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could try this one:

let result= [
  ["user1", "java"], 
  ["user2", "python"],
  ["user1", "java"], 
  ["user1", "C++"], 
  ["user1", "java"], 
  ["user2", "Python"]
];
const map = new Object();
result.forEach((item) => {
  if(!map[item[0]]){
    map[item[0]] = new Array();
  }
  const user = map[item[0]];
  const language = (item[1] || '').toLowerCase().trim();
  if(user.indexOf(language) < 0){
    user.push(language);
  }
});
result = new Array();
Object.keys(map).forEach((user) => {
  map[user].forEach((language) => {
    result.push([user,language]);
  });  
});
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can create a Set out of the array by joining the user and language strings, which would remove the duplicates. And then simply split the strings back to get the desired array.

const 
  result = [["user1", "java"], ["user2", "python"], ["user1", "java"], ["user1", "c++"], ["user1", "java"], ["user2", "python"]],
  uniques = Array.from(new Set(result.map((res) => res.join("-")))).map(
    (data) => data.split("-")
  );

console.log(uniques);

Relevant Documentations:

  • Array.from
  • Set
  • Array.prototype.map
  • Array.prototype.join
  • String.prototype.split
about 4 years ago · Juan Pablo Isaza Report

0

You can do this with a single loop over the array using reduce, join and Set as:

const result = [
    ['user1', 'java'],
    ['user2', 'python'],
    ['user1', 'java'],
    ['user1', 'C++'],
    ['user1', 'java'],
    ['user2', 'python'],
];

const [uniqueElements] = result.reduce((acc, curr) => {
        const [uniq, set] = acc;
        if (!set.has(curr.join(','))) {
            set.add(curr.join(','));
            uniq.push(curr);
        }
        return acc;
    },
    [[], new Set()],
);

console.log(uniqueElements);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!