Finding The Frequency Of Elements In A Sorted Array | FREE DSA Course in JAVA | Lecture 71

  Рет қаралды 14,828

TAP ACADEMY

TAP ACADEMY

Жыл бұрын

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

Пікірлер: 38
@PetPalsParadise_
@PetPalsParadise_ 3 ай бұрын
Sir please never stop uploading content like this 🙏🏻 thank you
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
Following code covers all cases : int i = 1; int freq = 1; for( i =1; i
@Itsallgoodmanedits
@Itsallgoodmanedits 3 ай бұрын
Aww man thanks for the code
@gottapukirankishorenaidu9411
@gottapukirankishorenaidu9411 18 күн бұрын
nice one bro its working
@saikatmaity441
@saikatmaity441 9 ай бұрын
good logic and nice explanation
@mehakarora3977
@mehakarora3977 Жыл бұрын
Thanku sir😊
@Rajkamal9Kamal908
@Rajkamal9Kamal908 2 ай бұрын
Beginner friendly ❤❤
@ajithsunkari2139
@ajithsunkari2139 Жыл бұрын
Thanku sir
@amitjain5273
@amitjain5273 10 ай бұрын
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)); }
@jasper5016
@jasper5016 10 ай бұрын
It's a very good solution.
@dasparijoy2610
@dasparijoy2610 Жыл бұрын
Hi I am Pranay, if we use "i
@babithatalari
@babithatalari 8 ай бұрын
We can't do that bro.....we get an error
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
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.
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
Code is giving ArrayIndexOutOfBoundsException for int a[] = {20,20,20,30,30,45,46,46};
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
Index 8 out of bounds for length 8
@user-tu2un9yp5b
@user-tu2un9yp5b 3 ай бұрын
Sir this logic not work when we give more then 8 elements in an array
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
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.
@pritamroy8792
@pritamroy8792 Жыл бұрын
If we put only same element in all index,it is giving result but showing "Index out of bound"
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
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.
@bibekamatya8588
@bibekamatya8588 4 ай бұрын
int freq =1; for(int i = 0; i
@the.sriya.guptaa
@the.sriya.guptaa Жыл бұрын
can't we do this with for loop? plzz reply
@mahaksoni7015
@mahaksoni7015 Жыл бұрын
yes we can
@itachi_terabapp
@itachi_terabapp Жыл бұрын
@@mahaksoni7015 how
@harshrajushire429
@harshrajushire429 Жыл бұрын
@@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 ☠
@itachi_terabapp
@itachi_terabapp Жыл бұрын
@@harshrajushire429 ma chutya hu ...bro ...
@AnoopSingh-fx2qy
@AnoopSingh-fx2qy Жыл бұрын
@@harshrajushire429 if the last elemnt repeats multiple time {20,20,40,50,50,30,30,30};
@GaneshGuturi
@GaneshGuturi 3 ай бұрын
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] }
@ShivaniKumari-qw3er
@ShivaniKumari-qw3er 10 күн бұрын
static void findfreq(int ar[ ]){ int prev=0,l=0,count=0; While( l
@ShivaniKumari-qw3er
@ShivaniKumari-qw3er 10 күн бұрын
One extra closing curly bracket is included by mistake
@user-on6dq2rf4p
@user-on6dq2rf4p 4 ай бұрын
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}");
@SahanasAllinOneChannel
@SahanasAllinOneChannel 4 ай бұрын
int[] a = {1, 2, 2, 3, 3, 3, 4}; for(int i=0; i
@user-on6dq2rf4p
@user-on6dq2rf4p 4 ай бұрын
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}"); }
@jasper5016
@jasper5016 10 ай бұрын
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++; } }
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
what about edge cases - if array has only 1 element or if last 2 elements are same ?
@KaranSharma-lm6eh
@KaranSharma-lm6eh 5 ай бұрын
your logic will be applicable only for case - when last 2 elements are different and arr.length >1.
80 Year Olds Share Advice for Younger Self
12:22
Sprouht
Рет қаралды 1,7 МЛН
Jumping off balcony pulls her tooth! 🫣🦷
01:00
Justin Flom
Рет қаралды 28 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
UNO!
00:18
БРУНО
Рет қаралды 3 МЛН
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 28 МЛН
Why UK 🇬🇧 is going Bankrupt? : Detailed Economic Case Study
20:37
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 807 М.
Java Tutorial for Beginners (Part 1) | TAP Academy
2:19:33
TAP ACADEMY
Рет қаралды 117 М.
Jumping off balcony pulls her tooth! 🫣🦷
01:00
Justin Flom
Рет қаралды 28 МЛН