3

You are given a non-empty list of integers. For this task, you should return a list consisting of only the non-unique elements in this list. To do so you will need to remove all unique elements (elements which are contained in a given list only once). When solving this task, do not change the order of the list. Example: [1, 2, 3, 1, 3] 1 and 3 non-unique elements and result will be [1, 3, 1, 3].

function nonUniqueElements(data) {
  var array = [];
  for (var i = 0; i < data.length; i++) {
      while (i >= 2) {
         array.push(i);
      }
      return data;
  }
}
Baris Demiray
  • 1,539
  • 24
  • 35
Edgar Kiljak
  • 1,160
  • 7
  • 27
  • All you do is check to see if `i` is greater than t2o, it does nothing to check to see if the number exists. – epascarello Nov 10 '16 at 22:20
  • would you be able to give me any hint without providing a solution please? – Edgar Kiljak Nov 10 '16 at 22:22
  • loop over the array and see if the element exists more than once. – epascarello Nov 10 '16 at 22:24
  • I thought im already looping through an array – Edgar Kiljak Nov 10 '16 at 22:27
  • Hmm, seems I read _this_ question wrong - for some reason, I thought it was "remove the duplicates entries, including all the duplications they have" but it's actually the opposite - "leave _only_ the duplicates". In which case, the Remove Duplicates question does work, simply do the _opposite_. – VLAZ Nov 10 '16 at 22:31
  • @EdgarKiljak but there is no check to see if the element exists, You look to see if the iteration count is greater than 2. Has nothing to do with the content in the array. – epascarello Nov 10 '16 at 22:34

2 Answers2

1

My solution:

  1. First loop over the array to count how much each number appears in the array
    by using map. Total time is O(n)
  2. And than loop again over the array and push to the new array the number, only if the current number appears more than 1 in the map. Total time is O(n).

     function nonUniqueElements(data) {
    
              //first loop over the array and find all duplications
              //create a map with all the numbers
              //the key will be the number, 
              //and the value will be how much each number appears in the array
              var map = {};
              for (var i=0; i < data.length; i++){
                if (map[data[i]] == undefined){
                  //the number does not exist in the map, than push to map
                   map[data[i]] = 0;
    
                } else {//the number alredy exists
                  //increase the counter
                   map[data[i]] = map[data[i]] +1;
                }
              }
    
    
              //now, loop over the array once again
              var nonUniqueArray = [];
                for (var i=0; i < data.length; i++){
                  //if the value of the current element is more than 1
                  if (map[data[i]] > 0){
                    //push to the new nonUniqueArray
                    nonUniqueArray.push(data[i]);
                  }
                }
    
              //return the  non unique array
              return nonUniqueArray;
            }
    

Hope it helps

Regina Kreimer
  • 126
  • 1
  • 2
  • 8
0

Use nested for loops that traverse the same array, in this case data.

First check if the index of each loop is the same. If it is do nothing. If they are not the same check the values. If the values are equal push to the array.

Steve G
  • 3
  • 4