Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
When to use and avoid array
Next article icon

Mean of an Array

Last Updated : 18 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an array arr[]. The task is to find the mean(floor value) of the array. 

Examples: 

Input: arr[] = [1, 3, 4, 2, 6, 5, 8, 7]
Output: 4
Explanation: Sum of the elements is 1 + 3 + 4 + 2 + 6 + 5 + 8 + 7 = 36, Mean = 36/8 = 4

Input: arr[] = [4, 4, 4, 4, 4]
Output: 4
Explanation: Sum of the elements is 4 + 4 + 4 + 4 + 4 = 20, Mean = 20/5 = 4

Writing Your Own Method

To find the Mean, simply find the sum of all the numbers present in the array. Then, simply divide the resulting sum by the size of the array

C++
#include <bits/stdc++.h> using namespace std;  int findMean(vector<int>& arr) {   	int n = arr.size();     int sum = 0;     for (int i = 0; i < n; i++)         sum += arr[i];      return sum / n; }  int main() {     vector<int> arr = { 1, 3, 4, 2, 7, 5, 8, 6 };   	 cout<<findMean(arr) << endl;     return 0; } 
C
#include <stdio.h>  int findMean(int arr[], int n) {     int sum = 0;     for (int i = 0; i < n; i++)         sum += arr[i];      return sum / n; }  int main() {     int arr[] = { 1, 3, 4, 2, 7, 5, 8, 6 };     int n = sizeof(arr) / sizeof(arr[0]);     printf("%d\n", findMean(arr, n));     return 0; } 
Java
import java.util.Arrays;  class GfG {    	static int findMean(int[] arr) {         int sum = 0;         for (int num : arr)             sum += num;          return sum / arr.length;     }      public static void main(String[] args) {         int[] arr = { 1, 3, 4, 2, 7, 5, 8, 6 };         System.out.println(findMean(arr));     } } 
Python
def findMean(arr):     return sum(arr) // len(arr)  if __name__ == "__main__":   arr = [1, 3, 4, 2, 7, 5, 8, 6]   print(findMean(arr)) 
C#
using System;  class GfG {     static int FindMean(int[] arr) {         int sum = 0;         foreach (int num in arr)             sum += num;          return sum / arr.Length;     }      static void Main() {         int[] arr = { 1, 3, 4, 2, 7, 5, 8, 6 };         Console.WriteLine(FindMean(arr));     } } 
JavaScript
function findMean(arr) {     let sum = 0;     for (let num of arr)         sum += num;      return Math.floor(sum / arr.length); }  // Driver Code const arr = [1, 3, 4, 2, 7, 5, 8, 6]; console.log(findMean(arr)); 

Output
4 

Time Complexity: O(n) 
Auxiliary Space: O(1)

Using Library Methods

  • accumulate in C++ STL
  • sum in Python
  • stream average in Java
  • reduce in JavaScript
C++
#include <bits/stdc++.h> using namespace std;  int findMean(vector<int>& arr) {     return accumulate(arr.begin(), arr.end(), 0) / arr.size(); }  int main() {     vector<int> arr = { 1, 3, 4, 2, 7, 5, 8, 6 };     cout << findMean(arr) << endl;     return 0; } 
Java
import java.util.Arrays; import java.util.OptionalDouble;  public class Main {     public static double findMean(int[] arr) {                  // Use stream to calculate mean         OptionalDouble sum = Arrays.stream(arr).average();                  // Return mean or 0 if array is empty         return sum.isPresent() ? sum.getAsDouble() : 0.0;      }      public static void main(String[] args) {         int[] arr = {1, 3, 4, 2, 7, 5, 8, 6};         System.out.println(findMean(arr)); // Print mean     } } 
Python
def find_mean(arr):     return sum(arr) // len(arr)  if __name__ == '__main__':     arr = [1, 3, 4, 2, 7, 5, 8, 6]     print(find_mean(arr)) 
C#
using System; using System.Linq;  public class MainClass {     public static double FindMean(int[] arr) {                  // Use LINQ to calculate mean         double mean = arr.Length > 0 ? arr.Average() : 0.0;                  // Return mean or 0 if array is empty         return mean;     }      public static void Main(string[] args) {         int[] arr = {1, 3, 4, 2, 7, 5, 8, 6};         Console.WriteLine(FindMean(arr)); // Print mean     } } 
JavaScript
function findMean(arr) {     return arr.reduce((a, b) => a + b, 0) / arr.length; }  const arr = [1, 3, 4, 2, 7, 5, 8, 6]; console.log(findMean(arr)); 

Output
4 




Next Article
When to use and avoid array
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Algorithms
  • Arrays
  • DSA
  • Mathematical
  • Sorting
  • Basic Coding Problems
  • maths-mean
  • median-finding
  • Order-Statistics
  • statistical-algorithms
Practice Tags :
  • Algorithms
  • Arrays
  • Mathematical
  • Sorting

Similar Reads

  • Median of an Array
    Given an array arr[], the task is to find the median of this array. The median of an array of size n is defined as the middle element when n is odd and the average of the middle two elements when n is even. Examples: Input: arr[] = [12, 3, 5, 7, 4, 19, 26]Output: 7 Explanation: Sorted sequence of gi
    11 min read
  • Mean of range in array
    Given an array arr[] of n integers and q queries represented by an array queries[][], where queries[i][0] = l and queries[i][1] = r. For each query, the task is to calculate the mean of elements in the range l to r and return its floor value. Examples: Input: arr[] = [3, 7, 2, 8, 5] queries[][] = [[
    12 min read
  • Mean of array using recursion
    Given an array of numbers, you are required to calculate the mean (average) using recursion. Note: The mean of an array is the sum of its elements divided by the number of elements in the array. Examples: Input: 1 2 3 4 5Output: 3Explanation: The sum of elements (15) divided by the number of element
    3 min read
  • Array Sum
    What is Array Sum?In context of Computer Science, array sum is defined as the sum of all the elements in an array. Suppose you have an array ARR[]={a1, a2, a3..., an}, let ArrSum be the array sum of this array, then by defination: ArrSum= a1 + a2 + a3 +...+ an Practice problems related to Array Sum:
    2 min read
  • When to use and avoid array
    Arrays are fundamental Data Structures in programming, offering a structured way to store and organize elements. However, like any tool, arrays are not a one-size-fits-all solution. Knowing when to embrace or avoid arrays is crucial for efficient and effective software development. In this explorati
    3 min read
  • Arrays and Strings in C++
    Arrays An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar types of elements as in the data type must be the same for all elements. They can be used to store the collection
    5 min read
  • Anonymous Array in Java
    An array in Java without any name is known as an anonymous array. It is an array just for creating and using instantly. Using an anonymous array, we can pass an array with user values without the referenced variable. Properties of Anonymous Arrays: We can create an array without a name. Such types o
    2 min read
  • Median of sliding window in an array | Set 2
    Prerequisites: Policy based data structure, Sliding window technique.Given an array of integer arr[] and an integer K, the task is to find the median of each window of size K starting from the left and moving towards the right by one position each time.Examples: Input: arr[] = {-1, 5, 13, 8, 2, 3, 3
    14 min read
  • Optimal Array
    Given a sorted array A[] of length N. For each i(0 ≤ i ≤ n-1), you have to make all the elements of the array from index 0 to i equal, using the minimum number of operations. In one operation you either increase or decrease the array element by 1. the task is to return a list that contains the minim
    7 min read
  • Find the mean vector of a Matrix
    Given a matrix of size M x N, the task is to find the Mean Vector of the given matrix. Examples: Input : mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : Mean Vector is [4 5 6] Mean of column 1 is (1 + 4 + 7) / 3 = 4 Mean of column 2 is (2 + 5 + 8) / 3 = 5 Mean of column 3 is (3 + 6 + 9) / 3 = 6
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences