-2

I have homework to write a function that will be called with 2 parameters:

a is a list of numbers.
amount represents the count of the numbers in the array.

The function should return the number in the list that occurs amount times.

For example , if a = [5,5,5,3,2,1,1], and amount = 2, the function should return 1, because there are only two ones in the array. If amount = 3, the function should return 5 , if amount = 6, the function will return 0 since there are numbers that occur six time.

jo_va
  • 13,504
  • 3
  • 23
  • 47
Dani Tsez
  • 21
  • 2
  • is it sum or count? – arun kumar Feb 11 '19 at 11:07
  • 3
    What have you tried? You're not putting any effort into this. – TKoL Feb 11 '19 at 11:07
  • 1
    Please share what you have tried as asking to code from scratch is not suitable on this site. – vahdet Feb 11 '19 at 11:07
  • 1
    Welcome to Stack Overflow! Please take the [tour], look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Assignments aren't usually arbitrary; your instructor, tutorial, or course will have covered the necessary topics to make it possible for you to do this. **Review your course materials, class notes, etc.,** and give it a try. *If* you run into a *specific* problem, research it thoroughly, [search thoroughly here](/help/searching), and if you're still stuck post your code and a description of the problem. People will be glad to help. – T.J. Crowder Feb 11 '19 at 11:07
  • 2
    https://meta.stackexchange.com/questions/18242/what-is-the-policy-here-on-homework – George Feb 11 '19 at 11:08
  • Possible duplicate of [How to count duplicate value in an array in javascript](https://stackoverflow.com/questions/19395257/how-to-count-duplicate-value-in-an-array-in-javascript) – Daan Feb 11 '19 at 11:16
  • hey sorry guys,this is what I tried so far: uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"]; var count = {}; uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;}); console.log(count) but he said its not valid because he wants it with the "amount" function and I have zero clues – Dani Tsez Feb 11 '19 at 11:17
  • You can edit the question @DaniTsez to include your code. If you indent the code 4 spaces (or select it and type command-k) it will format it as code. – Mark Feb 11 '19 at 11:18

4 Answers4

1

try this one

var max=3;
var array=[5,5,5,3,2,1,1];
console.log(verify(array,max))
function verify(array,max) {
  var match=0;
     array.map(obj => {
    if(array.filter(x => x === obj).length == max)
      match= obj;
    });
    return match;
}
Raviteja V
  • 454
  • 5
  • 11
0

Here is a way to get the values by count using reduce.

Since there may be more than one element matching the count, or even 0, an array is returned.

reduce is used to build a map (object) of the unique items to their counts. We the find the map entries where the counts match, and the keys (unique items) for these entries are returned.

const count = (arr, num) => {
  const count = arr.reduce((count, x) => {
    count[x] = (count[x] || 0) + 1;
    return count;
  }, {});
  return Object.entries(count)
    .filter(([k, v]) => v === num)
    .map(([k, v]) => +k);
}

console.log(count([5,5,5,3,2,1,1], 1)); // [2, 3]
console.log(count([5,5,5,3,2,1,1], 3)); // [5]
console.log(count([5,5,5,3,2,1,1], 5)); // []
jo_va
  • 13,504
  • 3
  • 23
  • 47
0

make a array of unique values from real array. Then loop though it and filter() real array to check the count of elements.

const array = [5,5,5,3,2,1,1]
function count(arr,amount){
  const unique = [...new Set(arr)];
  
  for(let item of unique){
    if(arr.filter(num => num === item).length === amount)
      return item;
  }
  return 0;
}
console.log(count(array,1));
console.log(count(array,2));
console.log(count(array,3));
console.log(count(array,4));
console.log(count(array,5));
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

You should use below code. @RaviTeja's code is giving max number of array but you dont want it. You want to number which is same quantity with your "amount" parameter.This code provides this for you

function findingAmount() {  
  var numberContainer = [ 2, 3, 2,5,2,3 ];  
  console.log(findingAmountImp(numberContainer,1)); // result 5
  console.log(findingAmountImp(numberContainer,2)); // result 3
  console.log(findingAmountImp(numberContainer,3)); // result 2
}


function findingAmountImp(numbers,amount) {
var count=1;
  for (i=0; i<numbers.length; i++) {
       for (j=0; j<numbers.length; j++) {
       if(i===j){
           j=j+1;
           if(j<numbers.length){
                if(numbers[i]  === numbers[j])
                {
                    count++; 
                } 
            }else{
                for(k=0;k<i;k++){
                   if(numbers[i]  === numbers[k])
                    {
                        count++; 
                    } 
                }
            }
       }else{
           if(numbers[i]  === numbers[j])
                {
                    count++; 
                } 
       }
   }
   if( count === amount){

       return numbers[i]
   }  
   count=1;
  }
}
Ömür Alçin
  • 619
  • 7
  • 15