Showing posts with label Count duplicates. Show all posts
Showing posts with label Count duplicates. Show all posts

Sunday, July 3, 2022

Count duplicates(Hashing)

 Count duplicates(Hashing)

Problem Statement
Given an array of N elements, your task is to find the count of repeated elements. Print the repeated elements in ascending order along with their frequency.
Have a look at the example for more understanding.

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
           
        // Your code here
        Scanner inputTaker = new Scanner(System.in);
        int n = inputTaker.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = inputTaker.nextInt();
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            if (map.containsKey(arr[i])) {
                Integer prevCount = map.get(arr[i]);
                map.put(arr[i], prevCount + 1);
            } else {
                map.put(arr[i], 1);
            }
        }

        for (Integer number : map.keySet()) {
            if (map.get(number) > 1) {
                System.out.println(number + " " + map.get(number));
            }
        }

    }
}

Sunday, June 26, 2022

Count duplicates

Count duplicates

Problem Statement

Given an array of N elements, your task is to find the count of repeated elements. Print the repeated elements in ascending order along with their frequency.
Have a look at the example for more understanding.
import java.util.Arrays;
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
 
public class Main {
    // Function to display the repeated elements and their frequencies 
    static void displayOutput(int[] a){
        int i,j,frequency;
        for(i=0; i<a.length; i++){
            frequency = 1;
            for(j=i+1; j<a.length; j++){
                if(a[j] == a[i]){
                    frequency++;
                }
                else{
                    break;
                }
            }
            i=j-1;
            if(frequency > 1){
                System.out.println(a[i] 
                                   + " " + frequency);
            }
        }
    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);

      int N = sc.nextInt();
         
            
        int []a = new int[N];
        for(int i =0; i<N; i++){
        
          a[i] = sc.nextInt();
                 }
                
            
        
        Arrays.sort(a);
        displayOutput(a);
    }
}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...