Explore
ProGrad Programs
About Us

Edit
Reply




Edit

Finding the Sum of all Odd Frequency Elements in an Array | FACE Prep

Published on 09 Mar 2020

Program to find the sum of all odd frequency elements in an array is discussed here. An array of integers is given as input and the sum of elements having odd frequency is found and produced as output. This means elements which repeat an odd number of times in the given array will be identified and the sum of these will be the expected output.


For example, consider an array arr = {1, 2, 4, 5, 6, 3, 1, 2, 3, 3}


Here, elements 1 and 2 repeats twice (even), whereas elements 4, 5, 6 are present only once (odd) and element 3 occurs thrice (odd). Hence,

  • Elements having odd frequency : {4, 5, 6, 3, 3, 3}
  • Sum of elements having odd frequency: 24


Algorithm


  • Traverse the array from the beginning.
  • Store the frequency of elements in the array using maps in C++ with the array element in one column and its frequency in the other column.
  • Now, find the elements having an odd frequency.
  • Calculate the sum of elements having an odd frequency and return the sum.


Program to find the sum of all odd frequency elements in an array


C++
Python 3

Output
Enter the number of elements : 10
Input the array elements : 1 1 2 2 3 3 3 4 5 6
The Sum of Odd frequency elements : 24


Time complexity: O(n)


Recommended Programs






If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'c plus plus'
Articles Practice Exercises