Question: Given an array of n numbers. Give an algorithm for finding the element which appears maximum number of times in the array?

    Input: 3, 2, 1, 2, 2, 3
    Output: 2

    One simple and a brute force solution to this is, for each input element check whether there is any element with same value and for each occurrence, increment the counter. Each time, check the current counter with the max counter and update it if this value is greater than max counter. This we can solve just by using two simple for loops.

    #include<stdio.h>
    
    //to find the number with maximum duplicates in array
    int maxDuplicatesBruteForce(int arr[], int size)
    {
    	int counter = 0;
    	int max = 0;
    	
    	int index_max = 0; //to store the index of maximum duplicated element
    	
    	int i,j;
    	for(i=0;i<size;i++)
    	{
    		counter = 0;
    		for(j=0;j<size;j++)
    		{
    			//check for duplicates in the array
    			if(arr[i] == arr[j])
    				counter++;
    		}
    		//update the counter and max_index
    		if(counter > max)
    		{
    			max = counter;
    			index_max = i;
    		}
    	}
    	
    	//return the maximum duplicated element
    	return arr[index_max];
    }
    
    int main(void)
    {
    	int arr[] = {3, 2, 1, 2, 2, 3};
    
    	printf("Max duplicated = %d",maxDuplicatesBruteForce(arr, 6));
    
    	return 0;
    }
    

    Time Complexity:- O(n2)
    Space Complexity:- O(1)

    Click here for an even optimized approach.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES Another possible and better way to solve this problem based on a certain condition. Let us assume that the array elements are positive numbers and also all the elements are in the range 0 to n-1. For each element A[i], go to the array element whose index is A[i]. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES Now the question arises, if we can improve the complexity of the above problem. We discussed the most basic method to find duplicates here. The time complexity can be improved by sorting the given array. After sorting, all the elements with equal values come adjacent to each other. Now, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES This is one of the simplest problems. One obvious solution to this is, exhaustively searching for duplicated elements in the array. That means, for each input element check whether there is any element with the same value. This we can solve by using two simple for loops. The code …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Binary Search

    by nikoo28
    4 minutes read

    Binary Search only works when your input list of elements is already sorted. This is main and the most important condition for this search algorithm. Unlike Linear Search, we take advantage of the sorted nature of the array. We always search in the middle portion of the array. Working of Binary Search: Let us say we have this sample array. Note that this array is sorted. I want to search for the element 12 in this array. One way, I can proceed is by dividing the array in 2 halves. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Different types of loops in C

    by nikoo28
    3 minutes read

    There are 3 loops in C: for, while, do-while. Here is a bit detail about them:- A while loop will always evaluate the condition first. while (condition) { //gets executed after condition is checked } A do/while loop will always execute the code in the do{} block first and then evaluate the condition. do { //gets executed at least once } while (condition); A for loop allows you to initiate a counter variable, a check condition, and a way to increment your counter all in one line. for (int x …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Linear Search

    by nikoo28
    4 minutes read

    Linear Search is probably the easiest searching technique that is available in computer programming. You simply need to iterate over each element in a list sequentially and compare if it matches the element to search. Searching is a prime concept, as it gives you a starting point to your code. Even in real life, if you plan to go to a movie, a vacation or a shopping mall, the first thing you do is search for a location. Similarly, in programming you could have a search for anything. Google has …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More