Given an array where every element occurs a times, except one element which occurs b (a>b) times. Find the element that occurs b times.
Input : arr = [1, 1, 2, 2, 2, 3, 3, 3] a = 3, b = 2 Output : 1
Add each number once and multiply the sum by a, we will get a times the sum of each element of the array. Store it as a_sum. Subtract the sum of the whole array from the a_sum and divide the result by (a-b). The number we get is the required number (which appears b times in the array).
Please refer below article for more approaches.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the element that appears once in an array where every other element appears twice
- First element that appears even number of times in an array
- Reduce the array such that each element appears at most 2 times
- Reduce the array such that each element appears at most K times
- Check if max occurring character of one string appears same no. of times in other
- Remove characters from string that appears strictly less than K times
- Count all elements in the array which appears at least K times after their first occurrence
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Remove characters from a String that appears exactly K times
- Count number of times each Edge appears in all possible paths of a given Tree
- Find the element that appears once
- Find the element that appears once in a sorted array
- Maximum difference between two elements such that larger element appears after the smaller number
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Count subarrays consisting of only 0's and only 1's in a binary array
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Find the K-th minimum element from an array concatenated M times
- Find the only repeating element in a sorted array of size n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.