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

ความคิดเห็น • 36

  • @PetPalsParadise_
    @PetPalsParadise_ 2 หลายเดือนก่อน +4

    Sir please never stop uploading content like this 🙏🏻 thank you

  • @KaranSharma-lm6eh
    @KaranSharma-lm6eh 4 หลายเดือนก่อน +9

    Following code covers all cases :
    int i = 1;
    int freq = 1;
    for( i =1; i

  • @saikatmaity441
    @saikatmaity441 9 หลายเดือนก่อน +1

    good logic and nice explanation

  • @mehakarora3977
    @mehakarora3977 ปีที่แล้ว +2

    Thanku sir😊

  • @Rajkamal9Kamal908
    @Rajkamal9Kamal908 หลายเดือนก่อน

    Beginner friendly ❤❤

  • @ajithsunkari2139
    @ajithsunkari2139 ปีที่แล้ว

    Thanku sir

  • @amitjain5273
    @amitjain5273 10 หลายเดือนก่อน +1

    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 9 หลายเดือนก่อน +2

      It's a very good solution.

  • @dasparijoy2610
    @dasparijoy2610 ปีที่แล้ว +1

    Hi I am Pranay, if we use "i

    • @babithatalari
      @babithatalari 8 หลายเดือนก่อน +1

      We can't do that bro.....we get an error

    • @KaranSharma-lm6eh
      @KaranSharma-lm6eh 4 หลายเดือนก่อน

      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.

  • @user-tu2un9yp5b
    @user-tu2un9yp5b 2 หลายเดือนก่อน

    Sir this logic not work when we give more then 8 elements in an array

  • @pritamroy8792
    @pritamroy8792 ปีที่แล้ว +1

    If we put only same element in all index,it is giving result but showing "Index out of bound"

    • @KaranSharma-lm6eh
      @KaranSharma-lm6eh 4 หลายเดือนก่อน

      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.

  • @the.sriya.guptaa
    @the.sriya.guptaa ปีที่แล้ว +2

    can't we do this with for loop?
    plzz reply

    • @mahaksoni7015
      @mahaksoni7015 ปีที่แล้ว +1

      yes we can

    • @itachi_terabapp
      @itachi_terabapp ปีที่แล้ว

      @@mahaksoni7015 how

    • @harshrajushire429
      @harshrajushire429 ปีที่แล้ว +2

      @@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};

  • @KaranSharma-lm6eh
    @KaranSharma-lm6eh 4 หลายเดือนก่อน

    Code is giving ArrayIndexOutOfBoundsException for int a[] = {20,20,20,30,30,45,46,46};

    • @KaranSharma-lm6eh
      @KaranSharma-lm6eh 4 หลายเดือนก่อน

      Index 8 out of bounds for length 8

  • @KaranSharma-lm6eh
    @KaranSharma-lm6eh 4 หลายเดือนก่อน +1

    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.

  • @GaneshGuturi
    @GaneshGuturi 2 หลายเดือนก่อน

    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]
    }

  • @SahanasAllinOneChannel
    @SahanasAllinOneChannel 3 หลายเดือนก่อน

    int[] a = {1, 2, 2, 3, 3, 3, 4};

    for(int i=0; i

  • @bibekamatya8588
    @bibekamatya8588 3 หลายเดือนก่อน

    int freq =1;
    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}");
    }

  • @user-on6dq2rf4p
    @user-on6dq2rf4p 4 หลายเดือนก่อน +1

    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}");

  • @jasper5016
    @jasper5016 9 หลายเดือนก่อน

    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 4 หลายเดือนก่อน

      what about edge cases - if array has only 1 element or if last 2 elements are same ?

    • @KaranSharma-lm6eh
      @KaranSharma-lm6eh 4 หลายเดือนก่อน

      your logic will be applicable only for case - when last 2 elements are different and arr.length >1.