Skip to main content

Frequency Of Elements in an Array

 Algorithm

Step 1: Create a map of integer to store key-value pairs. map<int,int>

Step 2: Now iterate over all the elements of the array and count their frequency by the help of map

Step 3: Now make a iterator for map to iterate over the map items

Step 4: This is the final step. Just print all the elements and their frequency stored in map respectively


 Code





Comments

Popular posts from this blog

10 Tips for Writing Faster and More Efficient Code

 10 Tips for Writing Faster and More Efficient Code Writing efficient code may seem like an impossible task when you’re just starting out with your programming journey, but the truth is that there are several steps you can take to make your code run faster and use fewer resources. You don’t have to be some sort of genius or even have years of experience to achieve this – here are 10 simple tips for writing more efficient code! 1) Use Type Alias When you need to define your own data type, consider using a type alias. This saves you from having to repeatedly reference more verbose names like String or NSNumber. Using a type alias also helps reduce pollution of existing types, which can cause problems later on. For example, adding an int property on top of NSObject will break a lot of code that uses that class without changing its definition. 2) Use Option Auto or Type Keywords Most developers have different preferences, but it is important to use Option Auto or Type Keywords. This wi...

C code to calculate product of even digits of a given number

 C code to calculate product of even digits of a given number Code Steps: 1. First input the number 2. Now initialize a variable =1, that stores the multiplication  3. Now start a while loop until number = 0 4. Inside while loop just find out all the digits by finding the remainder & check it if it is divisible by 2  5. If the number is divisible by 2 then multiply that number with the variable that was initialize = 1 6. At last return the multiplication storing variable.  C Code Snippet #include <stdio.h> int main (){     int num ;     int product = 1 ;     printf ( "Enter the number: " );     scanf ( " %d " , & num );     int temp = num ;     while ( num != 0 ){                 int digit = num % 10 ;         num = num / 10 ;         if ( digit % 2 == 0 ){         ...