![]() |
In this article, We have given a String list and a Kth index in which we have to check that each item in a list at that particular index should be unique. If they all are unique then we will print true in the console else we will print false in the console. Example: Input: test_list = [“gfg”, “best”, “for”, “geeks”], K = 1 . These are the following approaches by using these we can solve this question: Table of Content Using for loop:In this approach, we iterate for each string and check whether the character is present in the empty array or not. if the character is present in the ‘res’ array it means the value is not unique and it will return false else it will iterate the whole array check for the unique array and return true. Example: This example shows the use of the above-explained approach.
Output The original list is : gfg,best,for,geeks Is Kth index all unique : true Using every() methodIn this approach, we are checking if the characters at the 2nd index in the array test_list are unique. It initializes a count object to store character occurrences, then checks if all counts are less than 2 using every(). It prints “Is Kth index all unique : true” if all characters are unique, and “Is Kth index all unique : false” otherwise. Example: This example shows the use of the above-explained approach.
Output The original list is : gfg,best,for,geeks Is Kth index all unique : true Using Set and Array FilterIn this approach, we can utilize a Set data structure to keep track of unique characters at the Kth index of each string in the given list. We iterate through the list, extract the character at the Kth index of each string, and add it to the Set. If any character is already present in the Set, it indicates that there is a duplicate, and we return false. Otherwise, we return true if all characters are unique. Example:
Output The original list is: gfg,best,for,geeks Is Kth index all unique: true Using Set and Array FilterIn this approach, we can utilize a Set data structure to keep track of unique characters at the Kth index of each string in the given list. We iterate through the list, extract the character at the Kth index of each string, and add it to the Set. If any character is already present in the Set, it indicates that there is a duplicate, and we return false. Otherwise, we return true if all characters are unique. Example:
Output false true Using a Hash Map for Frequency CountingIn this approach, we utilize a hash map (JavaScript object) to keep track of the frequency of characters at the Kth index of each string in the given list. This method involves iterating through the list and updating the frequency count of each character. If any character’s frequency becomes greater than 1, we return false. Otherwise, after the iteration, we return true if all characters are unique. Example:
Output false true |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |