How to get the common values in an array

Viewed 187

For example i am having an array of data as below

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]

i need to list the same data as below in javascript

var arrDataSorted = ["40-25","50-48","30-25","40-23","40-45","40-50","40-50"]

need only the common data that replicates also the null to be removed.

What is the best solution to solve this.

8 Answers

You can try using Array.prototype.filter() to remove null values and Set to get the unique values. Finally use the Spread syntax (...) to transform the set result into an array.

Try the following way:

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

var arrDataSorted = [...new Set(arrData.filter(i => i))];
console.log(arrDataSorted);

You can create a set from an array which will automatically remove duplicates:

let arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

let set = new Set(arrData);

This will still keep the null, which you can remove with a delete call, and convert back to array with the spread ... operator. The final code will be:

let set = new Set(arrData);
set.delete(null);
let distinctArr = [...set];

add the values into the set if the value is not null and convert it to array.

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

var setData = new Set();
for(var data of arrData) {
    if(data) {
        setData.add(data);
    }
}

var arrDataSorted = [...setData];
console.log(arrDataSorted);

Add this function to your code:

function removeCommonValues(arr) {
  let result = [];
  for(let i=0; i < arr.length-1; ++i) {
    if(result.includes(arr[i]) === false && arr[i] !== null)
      result.push(arr[i])
  }
  return result
}

Usage:

removeCommonValues(["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]) // Return ["40-25", "50-48", "30-25", "40-23", "40-45", "40-50"]
var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]
var set = new Set();
      for ( var i = 0 ; i< arrData.length;i++ ) {
           if(arrData[i]!==null) {
                set.add(arrData[i]);
           }
      }
var newArr = [...set]

You could use array built-in reducer method, in the next code i'm starting with an empty array, and i'm only returning the items that are not null and are not already in the array.

const data = arrData.reduce((state, value) => {
   if(value && !state.includes(value)) {
        return [...state, value];
   }

   return state;
}, [])

The function can be in a separated file to be reused between multiple pages. Then you can call that function to filter distinct values that are not null.

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

function fn(value,index,self){
return self.indexOf(value) === index && value;
 }

console.log(arrData.filter(fn));

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]

const output = [];
arrData.forEach(val => {
  if(output.indexOf(val) === -1 && val !== null) {
    output.push(val);
  }
});

console.log(output);

Related