0
const array = [
  [
    'USA',
    'Apple',
    'New York',
    'Military'
  ],
  [
    'INDIA',
    'Mango',
    'New delhi',
    'AirCraft'
   ],
  [
    'United Kingdom',
    'Apple',
    'London',
    'Fighter'
]
] 

        var outputArray = []; 
        
        var count = 0; 
        
        var start = false; 
        
        for (j = 0; j < array.length; j++) { 
            for (k = 0; k < outputArray.length; k++) { 
                if ( array[j][1] == outputArray[k][1] ) { 
                    start = true; 
                } 
            } 
            count++; 
            if (count == 0 && start == false) { 
                outputArray.push(array[j][1]); 
            } 
            start = false; 
            count = 0; 
        } 
        document.write(outputArray); 

I am trying to filter out non duplicate from array of array but didn't get success

I was expecting only

[ 'INDIA', 'Mango', 'New delhi', 'AirCraft' ],

It should only check with index of 1 if duplicate matches found just discard this from array and print rest.

2
  • why were you expecting only the second array? because the 1st and 3rd have one element in common? Commented Oct 13, 2020 at 10:32
  • Yes that's correct both have Apple in the index of 1 Commented Oct 13, 2020 at 11:33

3 Answers 3

1

Say you define a toLookup function as follows:

const toLookup = (arr, keySelector) => arr.reduce((map, curr)=>{
  const key=keySelector(curr);
  const a = map.has(key) ? map.get(key) : [];
  a.push(curr);
  map.set(key, a);
  return map;
}, new Map())

which generates a Map where values are grouped by the selected key.

Now all we have to do is find values in this map that have a length of precisely 1:

const array = [
  ["USA", "Apple", "New York", "Military"],
  ["INDIA", "Mango", "New delhi", "AirCraft"],
  ["United Kingdom", "Apple", "London", "Fighter"]
];

const lookup = toLookup(array, v => v[1])

const uniques = [...lookup.values()].filter(v => v.length === 1)

console.log(uniques)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for support, Its working well, can you do me a favour is it possible duplicate row could push to new array ?
0

Using Array.prototype.some(), you can check if the duplicated values are existed or not.

And using Array.prototype.findIndex, you can find the existed index.

Finally, using Array.prototype.reduce, you can get the result.

const array = [
  [
    'USA',
    'Apple',
    'New York',
    'Military'
  ],
  [
    'INDIA',
    'Mango',
    'New delhi',
    'AirCraft'
  ],
  [
    'United Kingdom',
    'Apple',
    'London',
    'Fighter'
  ]
];

const result = array.reduce((acc, cur) => {
  const existed = acc.findIndex((item) => item.some((subItem) => cur.includes(subItem)));
  if (existed >= 0) {
    acc.splice(existed, 1);
  } else {
    acc.push(cur);
  }
  return acc;
}, []);
console.log(result);

3 Comments

only return one value even if I change Apple to potato it should return all three as nothing match with any fruit array index
Its not working properly if I changed the values and put difference one i.e const array = [ [ 'tester', 'this one indexx', '3', '4' ], [ 'Test2', 'this is second indexx', '2', '5' ], [ 'Test3', 'this is third indexx', '1', '5' ] ]; this should return all array
For that example, it should return the first item only because second and third contains 5. And mine works as expected.
0

You can use Array.prototype.flat and a helper object to count the occurrences and then filter the array where the count is greater than 1.

let array = [
  [
    'USA',
    'Apple',
    'New York',
    'Military'
  ],
  [
    'INDIA',
    'Mango',
    'New delhi',
    'AirCraft'
  ],
  [
    'United Kingdom',
    'Apple',
    'London',
    'Fighter'
  ]
];
const counter = {}

array.flat().forEach(i => counter[i] ? counter[i]++ : counter[i] = 1);

const dupes =Object.entries(counter)
  .filter(([k, v]) => v > 1)
  .map(([k,v])=> k);
  
array = array.filter(i => !dupes.some(d=> i.includes(d)));

console.log(array)

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.