0

I am trying to work on a problem where I want to remove all the occurrences of similar value in an array

eg.

var sampleArr = ["mary","jane","spiderman","jane","peter"];

and I am trying to get result as => ["marry","spiderman","peter"]

how do I get this?

  • Check this: https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates or https://stackoverflow.com/questions/11246758/how-to-get-unique-values-in-an-array – Dheemanth Bhat Feb 26 '21 at 07:57
  • 2
    Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Wils Feb 26 '21 at 07:59
  • 1
    Does this answer your question? [How to get unique values in an array](https://stackoverflow.com/questions/11246758/how-to-get-unique-values-in-an-array) – Dheemanth Bhat Feb 26 '21 at 08:00
  • please To finalize the answer, Please remember to accept and vote up the answer if your original issue has been solved and then ask a new question if you have another issue:https://stackoverflow.com/help/someone-answers – Frenchy Mar 02 '21 at 09:23

6 Answers6

0

You can use filter()

var arr = ["mary","jane","spiderman","jane","peter"];
var unique = arr.filter((x, i) => arr.indexOf(x) === i);
console.log(unique);
Ankita Kuchhadiya
  • 1,255
  • 6
  • 16
0

var sampleArr = ["mary","jane","spiderman","jane","peter"];
var unique = [];
var itemCount = {};
sampleArr.forEach((item,index)=>{
  if(!itemCount[item]){
  itemCount[item] = 1;
  }
  else{
  itemCount[item]++;
  }
});
for(var prop in itemCount){
  if(itemCount[prop]==1){
   unique.push(prop);
  }
}
console.log(unique);

Check this.

anand shukla
  • 666
  • 5
  • 14
0

You can count the frequency of the character and just pick the character whose frequency is 1.

const arr = ["mary","jane","spiderman","jane","peter"];
      frequency = arr.reduce((o,ch) => {
        o[ch] = (o[ch] || 0) + 1;
        return o;
      }, {}),
      unique = Object.keys(frequency).reduce((r,k) => frequency[k] === 1? [...r, k]: r, []);
console.log(unique);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

You can use filter:

var sampleArr = ["mary","jane","spiderman","jane","peter"];
var result = sampleArr.filter((e,_,s)=>s.filter(o=>o==e).length<2);
// or with reduce and flatmap
const result1 = Object.entries(sampleArr.reduce((a,e)=>(a[e]??=0, a[e]++, a),{})).flatMap(([k,v])=>v==1 ? k: []);

console.log(result)
console.log(result1);
gorak
  • 5,233
  • 1
  • 7
  • 19
0

lot of solution, here easy solution to understand using match to have the occurence and filter to eliminate:

var arr = ['ab','pq','mn','ab','mn','ab'];
var st = arr.join(",");

result = arr.filter(it => {
    let reg = new RegExp(it, 'g');
  return st.match(reg).length == 1;
});
console.log(result);// here ["pq"]
Frenchy
  • 16,386
  • 3
  • 16
  • 39
0

filter seems to be your best bet here if you need extremely robust performance. No real need for jQuery in this instance. Personally, I would opt for readability for something like this and instead sort, set duplicates to null, and then remove all null values.

var sampleArr = ["mary","jane","spiderman","jane","peter"];
var result = sampleArr.sort().forEach((value, index, arr) => {
    // if the next one is the same value, 
    // set this one to null
    if(value === arr[value + 1])
        return arr[index] = null;

    // if the previous one is null, and the next one is different, 
    // this is the last duplicate in a series, so should be set to null
    if(arr[value - 1] === arr[index + 1] !== value)
        return arr[index] = null;

    return
    
})
.filter(value => value === null) //remove all null values

console.log(result);
mindfullsilence
  • 344
  • 9
  • 23