JavaScript Program to Check if Kth Index Elements are Unique

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”, “Beginner”], K = 1 .
Output: False
Explanation: e occurs as 1st index in both best and Beginner.
Input: test_list = [“gfg”, “best”, “Beginner”], K = 2
Output: True
Explanation: g, s, e, all are unique.

These are the following approaches by using these we can solve this question:

Table of Content

  • Using for loop:
  • Using every() method
  • Using Set and Array Filter

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.

Javascript
// Initializing list
let test_list = ["gfg", "best", "for", "Beginner"];

// Printing original list
console.log("The original list is : " + test_list);

// Initializing K
let K = 2;

let res = [];
let flag = true;
for (let i = 0; i < test_list.length; i++) {
    let ele = test_list[i];

    // Checking if element is repeated
    if (res.includes(ele.charAt(K))) {
        flag = false;
        break;
    } else {
        res.push(ele.charAt(K));
    }
}

// Printing result
console.log("Is Kth index all unique : " + flag);

Output
The original list is : gfg,best,for,Beginner
Is Kth index all unique : true

Using every() method

In 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.

Javascript
// Initializing list
let test_list = ["gfg", "best", "for", "Beginner"];

// Printing original list
console.log("The original list is : " + test_list);

// Initializing K
let K = 2;

// Getting count of each Kth index item
let count = {};
for (let i = 0; i < test_list.length; i++) {
    let sub = test_list[i];
    let char = sub.charAt(K);
    count[char] = (count[char] || 0) + 1;
}

// Extracting result
let res = Object.values(count).every((val) => val < 2);

// Printing result
console.log("Is Kth index all unique : " + res);

Output
The original list is : gfg,best,for,Beginner
Is Kth index all unique : true

Method 3: Using Set and Array Filter

In 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:

JavaScript
// Initialize the list
let test_list = ["gfg", "best", "for", "Beginner"];

// Print the original list
console.log("The original list is: " + test_list);

// Initialize K
let K = 2;

// Use Set to store unique characters
let charSet = new Set();

// Iterate through the list and check for uniqueness
let isUnique = test_list.every((str) => {
    let char = str.charAt(K);
    if (charSet.has(char)) {
        return false; // If character is already present, return false
    } else {
        charSet.add(char); // Add character to Set
        return true;
    }
});

// Print the result
console.log("Is Kth index all unique: " + isUnique);

Output
The original list is: gfg,best,for,Beginner
Is Kth index all unique: true




Contact Us