Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

16 May 2024

Find All Repeating and Non-Repeating Elements

FIND ALL REPEATING AND NON-REPEATING ELEMENT 

In programming, identifying repeating and non-repeating elements in an array is a common problem with practical applications in data analysis, optimization, and more. This task involves separating elements based on their occurrence count, making it an essential exercise in mastering data structures and algorithms.

How to Solve the Problem?

  1. Input: Take an array of integers.
  2. Process:
    • Use a hash map to store the frequency of each element.
    • Traverse the array and update the count of each number in the hash map.
  3. Output:
    • Traverse the hash map to identify and print:
      • Elements with a count greater than 1 (repeating).
      • Elements with a count equal to 1 (non-repeating).

 import java.util.*;


class HelloWorld {

    public static void main(String[] args) {

        int n=sc.nextInt();

        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();

        for(int i=0;i<n;i++){

            int temp=sc.nextInt();

            if(map.containsKey(temp)){

                int value=map.get(temp);

                map.replace(temp,value++);

            }

            else{

                map.put(temp,1);

            }

        }

        System.out.print("repeating elements");

        for (Map.Entry<Integer, Integer> e : map.entrySet()) 

        {

            if(e.getValue()>1){

                System.out.print(e.getKey());

            }

        }

        System.out.println("non-repeating");

        for (Map.Entry<Integer, Integer> e : map.entrySet()) 

        {

            if(e.getValue()==1){

                System.out.print(e.getKey());

            }

        }

    }

}

Applications of Finding Repeating and Non-Repeating Elements

  • Data cleaning in databases.
  • Detecting duplicates in lists.
  • Problem-solving in competitive programming.

Find The Median of The Given Array

FIND THE MEDIAN OF THE GIVEN ARRAY

The median is a fundamental statistical measure that represents the middle value of a dataset when arranged in order. It divides the dataset into two equal halves, making it a robust measure for understanding central tendency, especially in the presence of outliers.

What is the Median?

  • For an odd-sized array, the median is the middle element.
    Example: In [1, 3, 5, 7, 9], the median is 5.
  • For an even-sized array, the median is the average of the two middle elements.
    Example: In [1, 3, 5, 7], the median is (3+5)/2=4(3 + 5) / 2 = 4.

Steps to Find the Median

  1. Input: Start with an array of numbers.
  2. Sort: Arrange the array in ascending order.
  3. Determine:
    • If the array size is odd, pick the middle element.
    • If the array size is even, compute the average of the two middle elements.
CODE

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        int n=sc.nextInt();

        int arr[]=new int[n];

        for(int i=0;i<n;i++){

            arr[i]=sc.nextInt();

        }

        Arrays.sort(arr);

        if(n/2==0){

            System.out.print((double)arr[(n/2)-1]+arr[n/2]/2);

        }

        else{

            System.out.print(arr[n/2]);

        }

    }

}

Applications of the Median

  • Used in statistics to analyze datasets.
  • Essential in financial analysis, such as calculating income or expenditure trends.
  • Helps in signal processing and machine learning to filter noise.

Find The Average of Array

FIND THE AVERAGE OF ARRAY 

What is the Average?

The average (or mean) of a dataset is calculated by dividing the sum of all elements by the number of elements. It offers a quick way to understand the overall trend or distribution of values.

Formula:

Average=Sum of ElementsNumber of Elements\text{Average} = \frac{\text{Sum of Elements}}{\text{Number of Elements}}

Steps to Calculate the Average

  1. Input: Start with an array of numbers.
  2. Sum: Add up all the elements in the array.
  3. Divide: Divide the sum by the total number of elements.

Example:

For the array [10,20,30,40,50][10, 20, 30, 40, 50]

  • Sum = 10+20+30+40+50=150
  • Number of elements = 55
  • Average = 150/5=30150 / 5 = 30
CODE

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        int sum=0;

        int n=sc.nextInt();

        for(int i=0;i<n;i++){

            sum+=sc.nextInt();

        }

        System.out.print(sum/n);

    }

}

Find The Sum of Array

FIND THE SUM OF ARRAY

Calculating the sum of elements in an array is a common programming task. In this post, we will explore how to achieve this in Java step by step, using a simple and efficient approach.

The Problem

Given an array of integers, we want to find the sum of all the elements. The user provides the number of elements followed by the values of the elements.

Step-by-Step Explanation

  1. Importing Required Package:

    • The java.util.* package is imported to use the Scanner class for input handling.
  2. Reading Input:

    • Scanner sc = new Scanner(System.in);: A Scanner object is created to read user input.
    • int n = sc.nextInt();: Reads the number of elements in the array (size of the array).
  3. Calculating the Sum:

    • The for loop runs n times, reading each number using sc.nextInt() and adding it to the sum variable.
  4. Displaying the Result:

    • The final sum is printed using System.out.print(sum);.

CODE

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        int sum=0;

        int n=sc.nextInt();

        for(int i=0;i<n;i++){

            sum+=sc.nextInt();

        }

        System.out.print(sum);

    }

}

Remove Dupicate Value From Array

 import java.util.*;


class HelloWorld {

    public static void main(String[] args) {

        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();

        int n=sc.nextInt();

        for(int i=0;i<n;i++){

            int temp=sc.nextInt();

            if(!map.containsKey(temp)){

                map.put(temp,1);

            }

        }

        int arr[]=new int[map.size()];

        int j=0;

        for (Map.Entry<Integer, Integer> e : map.entrySet()) 

          arr[j++]=e.getKey();

        for(int i=0;i<j;i++){

            System.out.print(arr[i]);

        }

    }

}

Reverse The Array

 import java.util.*;


class HelloWorld {

    public static void main(String[] args) {

        int n=sc.nextInt();

        int arr[]=new int[n];

        for(int i=0;i<n;i++){

            arr[i]=sc.nextInt();

        }

        for(int j=n-1;j>=0;j--){

            System.out.print(arr[j]);

        }

    }

}