Use reduce for getting the result.

var arr = [["apple","ghana",15],["apple","brazil",16],["orange","nigeria",10],["banana","bangladesh",20],["banana","ghana",6]];

const res = arr.reduce((a, c) => {
	if (!a.find(v => v[0] === c[0])) {
		a.push(c);
	}
	return a;
}, []);

console.log(res);
.as-console-wrapper {
  min-height: 100% !important;
  top: 0;
}

Answer from Sajeeb Ahamed on Stack Overflow
๐ŸŒ
Blogger
sunnybahree.blogspot.com โ€บ 2016 โ€บ 01 โ€บ how-to-get-unique-items-or-values-from_14.html
Sunny Bahree: How to get unique items or values from Multidimensional Array or an Object JQuery
In this post, I will demonstrate how to get unique values from multidimensional array using JQuery. Basic knowledge of JQuery - To know more about the same, please see this site https://jquery.com/ CDN Reference of JQuery library which will be used in our JQuery script - JQuery CDN latest Stable Versions can be found here https://code.jquery.com/ Knowledge of Javascript/JQuery Arrays - To learn or know more about Arrays, please see this site http://www.w3schools.com/js/js_arrays.asp
Discussions

How to get only unique items in a multidimensional array with JavaScript/jquery? - Stack Overflow
I got for example a multidimensional array items with 2 dimensions. I get this array from a database, but it will fill up to 2600+ objects, but if i could some how unique this it would be around 30 More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 23, 2017
javascript - Function to return distinct values in a 2D array - Stack Overflow
Since this sounds like some sort of school assignment I will provide ideas not code. You should think about how a human looks through that 2D array and determines whether or not one of the arrays is unique or not. One has to look at each other row, for each row to determine if it is unique. More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Find a unique item based on two values in multi-dimensional array - Stack Overflow
I have an array called collection. This array contains a large number of arrays with a length of 12. Each item of the latter array has - among others - a source ID [0] and target ID [1] (pairs of s... More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 14, 2013
javascript - How to Count Unique Arrays in a Multidimensional Array - Stack Overflow
While reducing, you can check whether ... and then increment it by 1 (which acts as the default value for any new keys (i.e newly seen arrays)). Lastly, you can get the keys from your object which represent each unique array as strings using Object.keys(), and parse each back into ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 9, 2019
๐ŸŒ
Wikitechy
wikitechy.com โ€บ tutorials โ€บ javascript โ€บ unique-values-in-an-array
javascript tutorial - Unique values in an array - By Microsoft Award MVP - Learn in 30sec javascript - java script | wikitechy
To get an array with unique values we could do now this: var myArray = ['a', 1, 'a', 2, '1']; let unique = [...new Set(myArray)]; // unique is ['a', 1, 2, '1'] ... The constructor of Set takes an iterable object, like Array, and the spread operator ... transform the set back into an Array.
๐ŸŒ
CoreUI
coreui.io โ€บ blog โ€บ how-to-get-unique-values-from-a-javascript-array
How to Get Unique Values from a JavaScript Array ยท CoreUI
July 5, 2024 - The most efficient way to get unique values from an array is by using the Set object along with the spread operator. This method is concise and leverages ES6 features: const array = [1, 2, 2, 3, 4, 4, 5] const uniqueArray = [...new Set(array)] ...
๐ŸŒ
Medium
medium.com โ€บ @robert.roksela โ€บ 6-ways-to-remove-duplicates-from-an-array-in-javascript-es6-syntax-88cb3750a986
6 ways to remove duplicates from an Array in JavaScript / ES6 Syntax | by Robert Roksela | Medium
September 30, 2020 - If itโ€™s true then it returns only the accumulator, if itโ€™s false it returns the accumulator, which is expanded to a new array using spreading operator (โ€ฆ), and adds the โ€˜nextItemโ€™ to the new array. Everything is assigned to a new variable. ... For โ€ฆ of, similarly to forEach presented in this article, loops though all values of the โ€˜basedArrayโ€™ pushing unique elements to the freshly declared โ€˜targetArrayโ€™, which is outside of the loop.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 20355058 โ€บ how-to-get-only-unique-items-in-a-multidimensional-array-with-javascript-jquery
How to get only unique items in a multidimensional array with JavaScript/jquery? - Stack Overflow
May 23, 2017 - You're making a mistake in pushing them onto an array in the first place. You should be creating an associative array, with the key field the unique aspect.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-get-all-unique-values-remove-duplicates-in-a-javascript-array
JavaScript โ€“ Unique Values (remove duplicates) in an Array | GeeksforGeeks
November 14, 2024 - These are the following methods to get all non-unique values from an array:Table of ContentUsing Array Slice() MethodUsing for loopUsin ... In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-get-all-unique-values-remove-duplicates-in-a-javascript-array
JavaScript - Unique Values (remove duplicates) in an Array - GeeksforGeeks
July 11, 2025 - // Given array let a = [10, 20, ... } } // Display updated array console.log("Updated Array: ", a1); ... The array.filter() method is used to create a new array from an existing array consisting of only those elements ...
Top answer
1 of 2
2

You can map each inner array to a stringified version of itself using .map(JSON.stringified). Now, using this new array, you can reduce it to an object which contains each stringified array as a key, and keeps the number of occurrences as its value. While reducing, you can check whether or not the object's key has already been set using a[k] = (a[k] || 0)+1. If it has already been set, it will use the current number stored at the key and increment it by 1, if it hasn't already been set it will set it equal to zero, and then increment it by 1 (which acts as the default value for any new keys (i.e newly seen arrays)).

Lastly, you can get the keys from your object which represent each unique array as strings using Object.keys(), and parse each back into a non-stringified array using JSON.parse. You can get the counts from your array by using Object.values() as this will get all the values (ie: the counters) of your reduced object and put them into an array.

See example below:

const arr = [[1,2], [1,2], [1,3], [1,4], [1,4], [1,4]]; 

const arr_str = arr.map(JSON.stringify);
const arr_map = arr_str.reduce((a, k) => (a[k] = (a[k] || 0) + 1, a), {});
const uniqueArrays = Array.from(Object.keys(arr_map), JSON.parse);
const theCount = Object.values(arr_map);

console.log(uniqueArrays);
console.log(theCount);

2 of 2
1

you can use below code

var arr = [[1,2], [1,2], [1,3], [1,4], [1,4], [1,4]]; 
var uniqueArrays = []; 
var theCount = [];
var test = [], obj ={};
arr.forEach(val => {
    if(test.indexOf(val.toString()) == -1){
        test.push(val.toString());
        obj[val.toString()] = 1;
        uniqueArrays.push(val);
    }else{
        obj[val.toString()] += 1;

    }

})
theCount = Object.values(obj);
console.log(uniqueArrays);
console.log(theCount);

Hope it will help you.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 41467513 โ€บ unique-value-in-multidimentional-array-in-javascript
jquery - Unique value in multidimentional array in javascript - Stack Overflow
In js, I create an array multidimention use my code like this : result.each(function(i, element){ init.push({ label : $(this).data('label'), value : $(this).val(), }); }); T...
Top answer
1 of 16
445

Here's a much cleaner solution for ES6 that I see isn't included here. It uses the Set and the spread operator: ...

var a = [1, 1, 2];

[... new Set(a)]

Which returns [1, 2]

2 of 16
302

Or for those looking for a one-liner (simple and functional) compatible with current browsers:

let a = ["1", "1", "2", "3", "3", "1"];
let unique = a.filter((item, i, ar) => ar.indexOf(item) === i);
console.log(unique);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Update 2021 I would recommend checking out Charles Clayton's answer, as of recent changes to JS there are even more concise ways to do this.

Update 18-04-2017

It appears as though 'Array.prototype.includes' now has widespread support in the latest versions of the mainline browsers (compatibility)

Update 29-07-2015:

There are plans in the works for browsers to support a standardized 'Array.prototype.includes' method, which although does not directly answer this question; is often related.

Usage:

["1", "1", "2", "3", "3", "1"].includes("2");     // true

Pollyfill (browser support, source from mozilla):

// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {

      // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }

      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;

      // 5. If n โ‰ฅ 0, then
      //  a. Let k be n.
      // 6. Else n < 0,
      //  a. Let k be len + n.
      //  b. If k < 0, let k be 0.
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

      // 7. Repeat, while k < len
      while (k < len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(searchElement, elementK) is true, return true.
        // c. Increase k by 1.
        // NOTE: === provides the correct "SameValueZero" comparison needed here.
        if (o[k] === searchElement) {
          return true;
        }
        k++;
      }

      // 8. Return false
      return false;
    }
  });
}
๐ŸŒ
DEV Community
dev.to โ€บ phibya โ€บ methods-to-get-unique-values-from-arrays-in-javascript-and-their-performance-1da8
Methods to get unique values from arrays in Javascript and their performance - DEV Community
February 21, 2022 - It is clear that the more duplication we have in the array, the faster the code runs. It is also obvious that using Array.prototype.reduce and Set is the fastest among all. Bonus: Getting unique values from an array of objects using multiple-level object keys (nested properties):
๐ŸŒ
Appdividend
appdividend.com โ€บ 2022 โ€บ 06 โ€บ 04 โ€บ how-to-get-distinct-values-from-array-in-javascript
Getting Unique Values (Remove Duplicates) in an Array in JavaScript
November 7, 2025 - Here are five ways to get all unique values from an array in JavaScript : Using new Set() constructor ,Using filter() + indexOf() methods ,Using filter() method , Using Set and Array.from() a method and Defining custom Array Unique Prototype
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ how-do-i-get-all-of-the-unique-values-in-a-javascript-array-remove-duplicates
how do i get all of the unique values in a javascript array remove duplicates
April 12, 2024 - The reduce() method executes a reducer function on each element of the array, resulting in a single output value. By combining it with includes(), we can efficiently accumulate unique values.
Top answer
1 of 2
9

Looks like you overcomplicated it a bit ;)

function unite() {
    return [].concat.apply([], arguments).filter(function(elem, index, self) {
        return self.indexOf(elem) === index;
    });
}


res = unite([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]);
document.write('<pre>'+JSON.stringify(res));

Explanations

We split the problem into two steps:

  • combine arguments into one big array
  • remove non-unique elements from this big array

This part handles the first step:

[].concat.apply([], arguments)

The built-in method someArray.concat(array1, array2 etc) appends given arrays to the target. For example,

[1,2,3].concat([4,5],[6],[7,8]) == [1,2,3,4,5,6,7,8]

If our function had fixed arguments, we could call concat directly:

function unite(array1, array2, array3) {
    var combined = [].concat(array1, array2, array3);
    // or
    var combined = array1.concat(array2, array3);

but as we don't know how many args we're going to receive, we have to use apply.

 someFunction.apply(thisObject, [arg1, arg2, etc])

is the same as

 thisObject.someFunction(arg1, arg2, etc)

so the above line

 var combined = [].concat(array1, array2, array3);

can be written as

 var combined = concat.apply([], [array1, array2, array3]);

or simply

 var combined = concat.apply([], arguments);

where arguments is a special array-like object that contains all function arguments (actual parameters).

Actually, last two lines are not going to work, because concat isn't a plain function, it's a method of Array objects and therefore a member of Array.prototype structure. We have to tell the JS engine where to find concat. We can use Array.prototype directly:

 var combined = Array.prototype.concat.apply([], arguments);

or create a new, unrelated, array object and pull concat from there:

 var combined = [].concat.apply([], arguments);

This prototype method is slightly more efficient (since we're not creating a dummy object), but also more verbose.

Anyways, the first step is now complete. To eliminate duplicates, we use the following method:

 combined.filter(function(elem, index) {
     return combined.indexOf(elem) === index;
 })

For explanations and alternatives see this post.

Finally, we get rid of the temporary variable (combined) and chain "combine" and "dedupe" calls together:

return [].concat.apply([], arguments).filter(function(elem, index, self) {
    return self.indexOf(elem) === index;
});

using the 3rd argument ("this array") of filter because we don't have a variable anymore.

Simple, isn't it? ;) Let us know if you have questions.

Finally, a small exercise if you're interested:

Write combine and dedupe as separate functions. Create a function compose that takes two functions a and b and returns a new function that runs these functions in reverse order, so that compose(a,b)(argument) will be the same as b(a(argument)). Replace the above definition of unite with unite = compose(combine, dedupe) and make sure it works exactly the same.

2 of 2
0

You can also try this :

var Data = [[1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]]

var UniqueValues = []

for (var i = 0; i < Data.length; i++) {
 UniqueValues = [...new Set(UniqueValues.concat(Data[i]))]
}

console.log(UniqueValues)