Count the Occurrences of a Value in an Array | C Programming Example

Поділитися
Вставка
  • Опубліковано 8 січ 2025

КОМЕНТАРІ • 22

  • @justcurious1940
    @justcurious1940 Рік тому +2

    Nice Serie , Thanks :
    int Occurences (int *array,int length,int to_find){
    int count = 0;
    for(int i = 0 ; i < length ; i++){
    if(array[i] == to_find){
    ++count;
    }
    }
    return count;
    }

  • @naboulsikhalid7763
    @naboulsikhalid7763 Рік тому +3

    very enjoyable to watch and learn at the same time. I would like to see if at the end of each lesson you throw an exercise with additional complexity for practice and get more interaction with your followers(Learners like me). thank you

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      I'm glad you enjoyed it! :-) Thanks for the suggestion, maybe one day I'll do something like that.

  • @Cooks_Games
    @Cooks_Games Рік тому +1

    These are great little exercises, but I find myself having to constantly pause to keep up with your speed

  • @architectCobi
    @architectCobi 2 роки тому +2

    Hi, great video!
    I've learned a lot from these exercise videos and i understand the logic as well. After each video, i try to create these codes on my own and it's been a great learning experience. Thankyou for sharing this information!!
    Quick question though: once I'm done with this exercise playlist, how do I progress further?
    Do these exercise questions get asked in Interviews?
    And could you share an example of these exercises are used in real life projects or applications?
    Thanks in advance:)

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      I would suggest progressing further by working on your own "side projects". Try to build something you're interested in building, for example a game, or some utility tool. There are lists of "project ideas" online. In the process of building a project you'll find that you'll need to self-learn other topics, but that's an incredibly useful process that's similar to working on "real-world commercial projects" where you'll need to do the same.
      Some of these exercises likely get asked in interviews, yes.
      Regarding real-world applications, in the case of some exercises there are many, and in others there are not many at all. :-) So something like Fizz Buzz is based off a children's game and is helpful in terms of teaching us how to think through a simple problem. Other exercises like say Quicksort are used all the time as part of standard libraries to sort anything from a list of files to a playlist, etc.

  • @andrejpoljakovic5482
    @andrejpoljakovic5482 Рік тому +1

    Great video. I have a question. How do you find the number of all repeated values in an array which size is given by the user and the numbers are random numbers? Example n=10, a[n]={1,1,2,2,3,3,3,4,4,4} and i need it to print the number of occurrences of every number in the array. Thank you!

    • @Mkhuss
      @Mkhuss Рік тому

      u have to hash in this case

    • @justcurious1940
      @justcurious1940 Рік тому

      Each time u find an occurrence u will have to update the array, and this process could be a pain in C , kind off , I'm not sure , I' don't wanna give this one a shot right now , but if u insist I can sacrifice my time to solve this one ;

    • @rupamsengupta1467
      @rupamsengupta1467 Рік тому

      please do it for us😅@@justcurious1940

  • @nour1144
    @nour1144 Рік тому

    What if we wanted the program to print which numbers were repeated without us typing the numbers

  • @zhenghaokang436
    @zhenghaokang436 2 роки тому +1

    How can i count all the numbers: 0~9; make the output like : 0s found 0; 1s found 1; 2s found 1; 3s found 0....... 9s found

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +3

      Great question Zhenghao! :-) There are a number of ways you could do this. One way would be to have 10 variables, one for each number, to keep a count of each number. You could then have inside the loop that loops through the array an if control structure like this:
      if (array[i] == 0) zero_counter++;
      else if (array[i] == 1) one_counter++;
      ...
      and so on
      ...
      You could use a switch here instead. You could also use an array to keep track of the counts using an array. So something like:
      int counts[10];
      where counts[0] keeps track of the count of occurrences of 0, counts[1] keeps track of the count of occurrences of 1, and so far.
      Those are just some ideas though. This video on counting each letter in a string might give you some ideas too: ua-cam.com/video/1OZMcC0euic/v-deo.html. :-)

  • @boborafg5632
    @boborafg5632 2 роки тому +1

    How can i count the values between 5 and 8 ( example)?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      Great question! :-) We could have the if-statement look something like this:
      if (array[i] >= 5 && array[i]

    • @boborafg5632
      @boborafg5632 2 роки тому

      @@PortfolioCourses thanks, but do u also know how i can get the first occurrence higher than 7 in a array.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      @@boborafg5632 That would probably be something like this:
      if (arrray[i] > 7) return array[i];
      or possibly return i; instead if you are looking for the index of that element.

    • @boborafg5632
      @boborafg5632 2 роки тому +1

      @@PortfolioCourses yes but then i get all values higher than 7
      . I want the first value higher than 7

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Well if you want the first value higher than 7, you either need to store that value in an array or return the value from a function, or stop the loop when it finds the value, or *something* like that. Exactly what you do is up to you, but that if-statement there is the general idea. :-)

  • @mdeliyas8838
    @mdeliyas8838 4 місяці тому

    It's called Frequency Counting.

  • @x9bq
    @x9bq Рік тому

    How to find items are less than 100 for example and print “ there 5 items less than 100 “

  • @chandu1969
    @chandu1969 11 місяців тому

    sir please say how to find all elements counts please sir🙏🙏🙏.