Remove duplicate elements from an array using AngularJS Last Updated : 07 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report We have given an array and the task is to remove/delete the duplicates from the array using AngularJS.Approach: The approach is to use the filter() method and inside the method, the elements that don't repeat themselves will be returned and the duplicates will be returned only once.Hence, a unique array will be made.Example 1: In this example, the character 'g' and 'b' are removed from the original array. HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function ($scope) { $scope.arr = ['g', 'a', 'b', 'c', 'g', 'b']; $scope.res = []; $scope.remDup = function () { $scope.res = $scope.arr .filter(function (item, pos) { return $scope.arr.indexOf(item) == pos; }) }; }); </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> Remove duplicate elements from the array in AngularJS </p> <div ng-app="app"> <div ng-controller="controller"> Original Array = {{arr}} <br><br> <button ng-click='remDup()'> Click here </button> <br><br> Final Array = {{res}}<br> </div> </div> </body> </html> Output:Example 2: This example does the case-sensitive comparison, so the elements like 'gfg' and 'GFG' will not be considered as duplicates. HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function ($scope) { $scope.arr = ['gfg', 'GFG', 'Gfg', 'gFG', 'gFg', 'gFg']; $scope.res = []; $scope.remDup = function () { $scope.res = $scope.arr .filter(function (item, pos) { return $scope.arr.indexOf(item) == pos; }) }; }); </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> Remove duplicate elements from the array in AngularJS </p> <div ng-app="app"> <div ng-controller="controller"> Original Array = {{arr}} <br> <br> <button ng-click='remDup()'> Click here </button> <br> <br> Final Array = {{res}}<br> </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to make empty an array using AngularJS ? P PranchalKatiyar Follow Improve Article Tags : AngularJS HTML-Misc AngularJS-Misc Similar Reads How to push elements in an array using AngularJS ? Given an array and the task is to perform the push operation on the array using AngularJS. The arr.push() method is used to push one or more values into the array, & new values will add at the end of an array. This method changes the length of the array by the number of elements added to the arr 2 min read How to Remove HTML element from DOM using AngularJS ? In this article, we will see how to remove particular HTML elements from the DOM with the help of AngularJS, along with understanding its implementation through the examples. For this, we can use the remove() method that helps to remove all the selected elements including all the text. Syntax: selec 2 min read How to make empty an array using AngularJS ? Given an array & the task is to make empty an array or delete all the elements from the array in AngularJS. In order to do this, there are 2 ways i.e., either use the [] notation to reinitialize the array which eventually removes all the elements from the array, or set the length of the array to 2 min read How to update an array element in AngularJS ? Given an array with an initial array of elements & the task is to update an array element using AngularJS. To update a particular item in an array, there are 2 ways, i.e., either by its value or by its index. Here, we will use the concept of the Property accessors method that is used to access t 2 min read How to empty the content of an element using AngularJS ? In this article, we will see how to remove the content of an element in HTML DOM using AngularJS. This task can be accomplished by implementing the jQuery empty() method that removes all child nodes and their content for the selected elements. Syntax: element.empty();Parameter: This method does not 2 min read How to Delete a Row from Table using AngularJS ? Given a HTML table and the task is to remove/delete the row from the table with the help of AngularJS.Approach: The approach is to delete the row from the array where it stored and served to the table data. When the user clicks on the button near to the table row, it passes the index of that table a 2 min read Like