Find Count Of Each Element from Array | Java Interview Questions and Answers

preview_player
Показать описание
In this video, we showed how to find the count of each element from the array and print it.

#javacodinginterview
#javainterviewquestions
#java
#javaprogramming
Рекомендации по теме
Комментарии
Автор

package com.method;

public class FrequencyOfElements
{
public static void main(String[] args)
{
int[] arr= {10, 20, 30, 10, 30, 40, 50, 50, 50, 60};
int[] count=new int[256];

for(int i=0;i<arr.length;i++)
{
count[arr[i]]++;
}
for(int i=0;i<count.length;i++)
{
if(count[i]!=0)
{
System.out.println(i +" : "+count[i]);
}
}
}
}

satishbhutawale
Автор

Static varibales cannot be declared inside methods or members of a class

revathir
Автор

may i know y the count of 10 is again printing as 2 so we neeed to set the value to -1 if the number is going to repeat again

rads
Автор

Remove static for y, you'll get output as 1

shivask
Автор

static variable is not possible, we need to remove either we can keep final

sathiskumarp
Автор

Int y is an instance variable, so it can't be static.

nanisai
Автор

Remove the static keyword from the y variable.

letsexplorewithsoumyaranja
Автор

Map<Integer, Long> numsInMap = Arrays.stream(nums).boxed()
-> x, Collectors.counting()));
-> System.out.println(x.getKey() + " -> " + x.getValue()));
for java 8

ashishsingh
Автор

static variable can't be declared in funtion scope right?

bheemarayaa
Автор

How to do this same using JDK 8 features???

rpstudio
Автор

Bro plz post python interview questions

rajuveerla
welcome to shbcf.ru