Home > OS >  filter the item whose first element is more than twice repeated in array
filter the item whose first element is more than twice repeated in array

Time:10-28

I have an array and want to filter the item whose first element is more than twice repeated in this array.

Friends, how can I do this?.

 arr=[
[a,1],
[a,2],
[a,4],
[b,2],
[C,2],
[d,2],
[e,2],
[e,2],
[n,2],
...
]

I want the result of the filter to be like below

 result=[
[a,1],
[a,2],
[a,4],
[e,2],
[e,2],
...
]

CodePudding user response:

arr=[['a',1],['a',2],['a',4],['b',2],['C',2],['d',2],['e',2],['e',2],['n',2]]; temp=arr.map(val=>val[0]).filter((a,i,aa)=>aa.indexOf(a)===i&&aa.lastIndexOf(a)!==i);
result=arr.filter((a, i,aa)=>temp.includes(aa[i][0]));
console.log(result)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

want to filter the item whose first element is more than twice repeated in this array.

To do that You need :

  1. make a hashed map with elements containing character with its times repeated.
  2. keep only elements from array whose repeated two or more times.
const arrays=[['a',1], ['a',2], ['a',4], ['b',2], ['C',2], ['d',2], ['e',2],['e',2], ['n',2]];

//utility function to get keys of each entry
const keys = arr => arr.map(char=>char[0]);

let chars = keys(arrays); // ['a', 'a', 'a', 'b', 'C', 'd', 'e', 'e', 'e', 'n']

// 1.
const hashMap = new Map();
chars.forEach(char=>{
    count = hashMap.get(char)||0;
    hashMap.set(char,   count);
});
const dict = [...hashMap].filter(([char, count])=>count>=2)

// 2.
uniqueChars = keys(dict);
const result = arrays.filter(([chr, num])=>uniqueChars.includes(chr))
console.log(result)

Result:

['a', 1]
['a', 2]
['a', 4]
['e', 2]
['e', 2]

CodePudding user response:

Check this out:

arr=[['a',1], ['a',2], ['a',4], ['b',2], ['C',2], ['d',2], ['e',2], ['e',2], ['n',2]];

 const reducedObject = arr
  .reduce((acc, [key, value]) => (
    acc[key] = acc[key] 
      ? [...acc[key], [key, value]] 
      : [[key, value]],
    acc
  ), {});

  console.log(reducedObject);
// {
//   a: [ [ 'a', 1 ], [ 'a', 2 ], [ 'a', 4 ] ],
//   b: [ [ 'b', 2 ] ],
//   C: [ [ 'C', 2 ] ],
//   d: [ [ 'd', 2 ] ],
//   e: [ [ 'e', 2 ], [ 'e', 2 ] ],
//   n: [ [ 'n', 2 ] ]
// }

const result = Object
  .values(reducedObject)
  .filter((values) => (values.length >= 2))
  .reduce((acc, values) => ([...acc, ...values]), []);

console.log(result);
// [ 
//   [ 'a', 1 ], 
//   [ 'a', 2 ], 
//   [ 'a', 4 ], 
//   [ 'e', 2 ], 
//   [ 'e', 2 ] 
// ]

Another, one-line-way solution:

const result2 = arr
  .map(([ key ]) => key)  //[ 'a', 'a', 'a', 'b', 'C', 'd', 'e', 'e', 'n' ]
  .filter((key, index, array) => array.indexOf(key) !== index)  //[ 'a', 'a', 'e' ]
  .filter((key, index, array) => array.indexOf(key) === index)  //[ 'a', 'e' ]
  .reduce((acc, key) => (  
    [...acc, ...arr.filter(([ itemKey ]) => (itemKey === key))]
  ), []);   
  
console.log(result2);
// [ [ 'a', 1 ], [ 'a', 2 ], [ 'a', 4 ], [ 'e', 2 ], [ 'e', 2 ] ]
  • Related