Finding The Frequency Of Elements In A Sorted Array | FREE DSA Course in JAVA | Lecture 71
ฝัง
- เผยแพร่เมื่อ 7 ก.พ. 2023
- We have to write a program in Java to find the frequency of all the elements of the given array.
Frequency means the number of times an element occurs in an array.
For eg, consider the array 20,20,30,30,30,40 the output will be:-
20 2
30 3
40 1
What's the logic for the program question.
You can simply find the solution by taking the frequency of element 1 as 1 by default and checking for further elements if they are equal to the previous element.
If yes increase the frequency variable by 1.
If the condition breaks, set the frequency variable to 1 again and do the same process.
In this way, you will be able to find the frequency of the elements.
There's one condition that we have missed in this logic.
Check out the complete program to know it and the code to write the program.
For more information, fill out this form: forms.gle/8eiUmM92Fx563Aen9
or call us at 8884881203
Facebook: / thetapacademy
Instagram: / tapacademy_online
Linkedin: / 73820805
Website: www.thetapacademy.com
#java #array #arrayjava #dsa #dsacourse #courseofdsa #datastructure #coding #programming
Sir please never stop uploading content like this 🙏🏻 thank you
Following code covers all cases :
int i = 1;
int freq = 1;
for( i =1; i
Aww man thanks for the code
nice one bro its working
good logic and nice explanation
Thanku sir😊
Beginner friendly ❤❤
Thanku sir
Sir, we can do this by a 2-pointer approach. As you had taught in the previous video.
public static void frequency(int arr[]) {
int firstElementIndex = 0;
int duplicateElementIndex = 1;
while (duplicateElementIndex < arr.length) {
if (arr[firstElementIndex] == arr[duplicateElementIndex]) {
duplicateElementIndex++;
} else {
System.out.println(arr[firstElementIndex] + " - " + (duplicateElementIndex - firstElementIndex));
firstElementIndex = duplicateElementIndex;
}
}
System.out.println(arr[firstElementIndex] + " - " + (duplicateElementIndex - firstElementIndex));
}
It's a very good solution.
Hi I am Pranay, if we use "i
We can't do that bro.....we get an error
when i=a.length, then while comparing a[i] == a[i-1] will return out of bounds exception as a[i] doesn't exist means index = length -1.
Sir this logic not work when we give more then 8 elements in an array
If we put only same element in all index,it is giving result but showing "Index out of bound"
yes, that is what i too have observed. Overall code won't work for different data sets. As i will be at 2 positions ahead and a[i-1] will return ArrayIndexOutOfBounds exception.
can't we do this with for loop?
plzz reply
yes we can
@@mahaksoni7015 how
@@itachi_terabapp
int ar[] = {20,20,40,30,30,30};
int freq =1;
int i=1;
for ( i= 1; i < ar.length; i++) {
if(ar[i-1] == ar[i]){
freq++;
}else{
System.out.println(ar[i-1] + " "+freq);
freq=1;
}
}
System.out.println(ar[i-1] + " "+freq);
o/p:
20 2
40 1
30 3
☠
@@harshrajushire429 ma chutya hu ...bro ...
@@harshrajushire429 if the last elemnt repeats multiple time
{20,20,40,50,50,30,30,30};
Code is giving ArrayIndexOutOfBoundsException for int a[] = {20,20,20,30,30,45,46,46};
Index 8 out of bounds for length 8
Please improve your logic as it is not working for different data sets such as if all the elements are duplicate in an Array, last 2 elements are same, in that case i will be at 2 positions ahead due to being incremented twice, one inside the inner while-loop and then after Sys.ot in the outer-while loop. So when the flow goes to if() loop- it checks for a[i-1] != a[i-2] but a[i-1] will throw indexOutOfBounds exception.
In language:
private func FrequencyOfElementsInSortedArray(){
let input:[Int] = [5, 5, 10, 10, 10, 20, 20] // given sorted array
let lastIndex = input.count - 1
var result = [Int: Int]() //hash map to store the value and frequency
result[input[0]] = 1
for i in 1...lastIndex{
if result.keys.contains(input[i]){
let val = result[input[i]] ?? 0
result[input[i]] = val + 1
}else{
result[input[i]] = 1
}
}
print("result = \(result)") //Output = [5: 2, 10: 3, 20: 2]
}
int[] a = {1, 2, 2, 3, 3, 3, 4};
for(int i=0; i
int freq =1;
for(int i = 0; i
int[] intArray = { 10, 10, 20, 20, 20, 30, 40 };
Dictionary elemFrequancy = new Dictionary();
foreach (var element in intArray)
{
if (!elemFrequancy.ContainsKey(element))
elemFrequancy.Add(element, 1);
else
elemFrequancy[element]++;
}
foreach (var item in elemFrequancy)
{
Console.WriteLine($"Element: {item.Key} Frequency: {item.Value}");
}
int[] intArray = { 10, 10, 20, 20, 20, 30, 40 };
int freq = 1, count = 1;
if (intArray.Length > 1)
{
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] == intArray[i - 1])
{
freq++;
count++;
}
else
{
Console.WriteLine($"Element: {intArray[i - 1]} Frequency: {freq}");
freq = 1;
count++;
if (intArray.Length == count)
Console.WriteLine($"Element: {intArray[i]} Frequency: {freq}");
}
}
}
else
Console.WriteLine($"Element: {intArray[0]} Frequency: {freq}");
Your tutorials are really very helpful. Thanks
Here is my solution -
private static void findFrequency() {
int[] arr = {5,5,7,10,10,21}; //{10,20,20,30,30,30,40};
int index = 0;
int freq = 1;
while(index < arr.length){
if(index < arr.length-1 && arr[index] == arr[index + 1]){
freq++;
} else {
System.out.println(arr[index] + " " + freq);
freq = 1; //reset frequency
}
index++;
}
}
what about edge cases - if array has only 1 element or if last 2 elements are same ?
your logic will be applicable only for case - when last 2 elements are different and arr.length >1.