Saumyajit Chowdhury AngularJS Interview

by Matthew Vasallo

HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<div ng-app="Myapp">
    <div ng-controller="MyFirstController">
        <ul id="theList" ng-repeat="item in collection track by $index">
            <li>{{ item }}</li>
        </ul>
        <button ng-click="checkDuplicate()">Check</button>
    </div>
</div>

JavaScript

/*
Write an algorithm that will find the first duplicate in a list on the page.
For example we would return 4, for the list above.
*/
var app = angular.module("Myapp", []);

app.controller("MyFirstController", ['$scope', function($scope){
    $scope.collection = ["6","3","1","4","7","4","2","8","9","2"]; 
    $scope.uniqueElements = [];
    $scope.checkDuplicate = function(){
        for(var i=0; i<$scope.collections.length; i++)
        {
          if($scope.collections[i+1] == $scope.collections[i])
          {
               $scope.unqueElements.push(i)   
          }
        }
        console.log($scope.unqueElements[0]);
         $scope.uniqueElements = _.uniq($scope.collection, function(item, key, a)             { 
        return item.a;
});

      
}

]);