Absolute distinct count in a Linked List
Last Updated :
16 Jan, 2023
Given a Linked List consisting of integers, the task is to print the number of distinct absolute values present in the Linked List.
Examples:
Input: -1 -> -2 -> 0 -> 4 -> 5 -> 8
Output: 6
Explanation:
Distinct absolute node values are {0, 1, 2, 4, 5, 8}
Input: -1 -> -1 -> -1 -> 0 -> 1
Output: 2
Explanation:
Distinct absolute node values are {0, 1}.
Approach: In order to solve this problem, we require a Set or a HashSet to store the distinct absolute values present in the Linked List. After traversing the entire linked list and inserting absolute values of every node in the Set, the size of the Set gives the number of absolute distinct values present in the Linked List.
Below is the implementation of the approach:
C++
// C++ Program to print the count
// of distinct absolute values
// in a linked list
#include <bits/stdc++.h>
using namespace std;
// Node of a singly
// linked list
struct Node {
int data;
Node* next;
};
// Function to insert a
// node at the beginning
// of a singly Linked List
void push(Node** head_ref, int new_data)
{
// Allocate node
Node* new_node = new Node();
// Insert the data
new_node->data = new_data;
// Point to the current head
new_node->next = (*head_ref);
// Make the current Node
// the new head
(*head_ref) = new_node;
}
// Function to return number of
// distinct absolute values
// present in the linked list
int distinctCount(Node* head_1)
{
Node* ptr = head_1;
unordered_set<int> s;
while (ptr != NULL) {
s.insert(abs(ptr->data));
ptr = ptr->next;
}
return s.size();
}
int main()
{
// Create the head
Node* head1 = NULL;
// Insert Nodes
push(&head1, -1);
push(&head1, -2);
push(&head1, 0);
push(&head1, 4);
push(&head1, 5);
push(&head1, 8);
int k = distinctCount(head1);
cout << k;
return 0;
}
Java
// Java program to calculate
// the count of distinct
// absolute values in a
// linked list
import java.util.*;
class GFG {
// Node of the singly
// linked list
static class Node {
int data;
Node next;
};
// Function to insert a node
// at the beginning of the
// singly Linked List
static Node push(Node head_ref,
int new_data)
{
// Allocate node
Node new_node = new Node();
// Insert the data
new_node.data = new_data;
// Point the current Node
// to the current head
new_node.next = (head_ref);
// Make the current node
// as the new head
(head_ref) = new_node;
return head_ref;
}
// Function to return the count of
// distinct absolute values
// present in the Linked list
static int distinctCount(Node head_ref1)
{
Set<Integer> s = new HashSet<Integer>();
Node ptr1 = head_ref1;
while (ptr1 != null) {
s.add(Math.abs(ptr1.data));
ptr1 = ptr1.next;
}
return s.size();
}
// Driver Code
public static void main(String args[])
{
// Create the head
Node head1 = null;
// Insert nodes to the
// Linked List
head1 = push(head1, -1);
head1 = push(head1, -2);
head1 = push(head1, 0);
head1 = push(head1, 4);
head1 = push(head1, 5);
head1 = push(head1, 8);
int ans = distinctCount(head1);
System.out.println(ans);
}
}
Python3
# Python3 program to calculate
# the count of distinct
# absolute values in a
# linked list
class Node:
def __init__(self, data):
self.data = data
self.next = next
# Function to insert a
# node at the beginning
# of the singly Linked List
def push( head_ref, new_data) :
# Allocate node
new_node = Node(0)
# Insert data
new_node.data = new_data
# Point to the head
new_node.next = (head_ref)
# Make the new Node
# the new head
(head_ref) = new_node
return head_ref
# Function to return the
# count of distinct absolute
# values in the linked list
def distinctCount(head_ref1):
s = set()
ptr1 = head_ref1
# set keeps all unique elements
while (ptr1 != None):
s.add(abs(ptr1.data))
ptr1 = ptr1.next
return len(s)
# Driver code
# Create the Head
head1 = None
# Insert nodes
head1 = push(head1, -1)
head1 = push(head1, -2)
head1 = push(head1, 0)
head1 = push(head1, 4)
head1 = push(head1, 5)
head1 = push(head1, 8)
ans = distinctCount(head1)
print(ans)
C#
// C# program to calculate the count
// of distinct absolute values in a
// linked list
using System;
using System.Collections.Generic;
class GFG{
// Node of the singly
// linked list
class Node
{
public int data;
public Node next;
};
// Function to insert a node
// at the beginning of the
// singly Linked List
static Node push(Node head_ref,
int new_data)
{
// Allocate node
Node new_node = new Node();
// Insert the data
new_node.data = new_data;
// Point the current Node
// to the current head
new_node.next = (head_ref);
// Make the current node
// as the new head
(head_ref) = new_node;
return head_ref;
}
// Function to return the count of
// distinct absolute values
// present in the Linked list
static int distinctCount(Node head_ref1)
{
HashSet<int> s = new HashSet<int>();
Node ptr1 = head_ref1;
while (ptr1 != null)
{
s.Add(Math.Abs(ptr1.data));
ptr1 = ptr1.next;
}
return s.Count;
}
// Driver Code
public static void Main(String []args)
{
// Create the head
Node head1 = null;
// Insert nodes to the
// Linked List
head1 = push(head1, -1);
head1 = push(head1, -2);
head1 = push(head1, 0);
head1 = push(head1, 4);
head1 = push(head1, 5);
head1 = push(head1, 8);
int ans = distinctCount(head1);
Console.WriteLine(ans);
}
}
// This code is contributed by Princi Singh
JavaScript
// JavaScript program to calculate
// the count of distinct absolute
// values in a linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
// Function to insert a node at the beginning of the linked list
push(newData) {
const newNode = new Node(newData);
newNode.next = this.head;
this.head = newNode;
}
// Function to return the count of distinct absolute values present in the linked list
distinctCount() {
const set = new Set();
let current = this.head;
while (current) {
set.add(Math.abs(current.data));
current = current.next;
}
return set.size;
}
}
const ll = new LinkedList();
ll.push(-1);
ll.push(-2);
ll.push(0);
ll.push(4);
ll.push(5);
ll.push(8);
console.log(ll.distinctCount());
// This code is contributed by lokesh.
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for set s.
Similar Reads
Count duplicates in a given linked list Given a linked list. The task is to count the number of duplicate nodes in the linked list. Examples: Input: 5 -> 7 -> 5 -> 1 -> 7 -> NULL Output: 2Input: 5 -> 7 -> 8 -> 7 -> 1 -> NULL Output: 1 Simple Approach: We traverse the whole linked list. For each node we check
9 min read
Count duplicates in a given circular linked list Given a circular linked list, the task is to check whether the given list has duplicates or not. Example: Input: list = {5, 7, 5, 1, 4, 4}Output: 2Explanation: The given list has 2 indices having integers which has already occurred in the list during traversal. Input: list = {1, 1, 1, 1, 1}Output: 4
6 min read
Sum of all distinct nodes in a linked list Given a linked list and it may consist of duplicate nodes. The task is to find the sum of non-duplicate nodes. Examples: Input: 1 -> 2 -> 1 -> 3 -> 4 -> 3 -> NULL Output: 6 2 and 4 are the only non-duplicate nodes and 2 + 4 = 6. Input: 1 -> 3 -> 1 -> 3 -> 1 -> 3 -
7 min read
Count Occurrences in a Linked List Given a singly linked list and a key, the task is to count the number of occurrences of the given key in the linked list. Example : Input : head: 1->2->1->2->1->3->1 , key = 1Output : 4 Explanation: key equals 1 has 4 occurrences.Input : head: 1->2->1->2->1, key = 3Outp
9 min read
Count nodes in Circular linked list Given a circular linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list.count nodes in the circular linked list.Using linear traversal - O(n) time and O(1) spaceThe idea is to start from the head node and traversing the list
5 min read
Find unique elements in linked list Given a linked list. We need to find unique elements in the linked list i.e, those elements which are not repeated in the linked list or those elements whose frequency is 1. If No such elements are present in list so Print " No Unique Elements". Examples: Input : 1 -> 4 -> 4 -> 2 -> 3 -
7 min read
Search an element in a Doubly Linked List Given a Doubly linked list(DLL) containing n nodes and an integer x, the task is to find the position of the integer x in the doubly linked list. If no such position found then print -1.Examples:Input: Linked List = 18 <-> 15 <-> 8 <-> 9 <-> 14, x = 8 Output: 3 Explanation: x
7 min read
Count minimum frequency elements in a linked list Given a linked list containing duplicate elements. The task is to find the count of all minimum occurring elements in the given linked list. That is the count of all such elements whose frequency is minimum in the matrix. Examples: Input : 1-> 2-> 2-> 3 Output : 2 Explanation: 1 and 3 are e
8 min read
Majority element in a linked list Given a linked list, find majority element. An element is called Majority element if it appears more than or equal to n/2 times where n is total number of nodes in the linked list. Examples: Input : 1->2->3->4->5->1->1->1->NULL Output : 1 Explanation 1 occurs 4 times Input :1
14 min read
Count occurrences of one linked list in another Linked List Given two linked lists head1 and head2, the task is to find occurrences of head2 in head1. Examples: Input: Head1 = 1->2->3->2->4->5->2->4, Head2 = 2->4Output: Occurrences of head2 in head1: 2 Input: Head1 = 3->4->1->5->2, Head2 = 3->4Output: Occurrences of Hea
15 min read