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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
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()
{
Node* head1 = NULL;
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
import java.util.*;
class GFG {
static class Node {
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
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();
}
public static void main(String args[])
{
Node head1 = null ;
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
class Node:
def __init__( self , data):
self .data = data
self . next = next
def push( head_ref, new_data) :
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def distinctCount(head_ref1):
s = set ()
ptr1 = head_ref1
while (ptr1 ! = None ):
s.add( abs (ptr1.data))
ptr1 = ptr1. next
return len (s)
head1 = None
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#
using System;
using System.Collections.Generic;
class GFG{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
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;
}
public static void Main(String []args)
{
Node head1 = null ;
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);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
class LinkedList {
constructor() {
this .head = null ;
}
push(newData) {
const newNode = new Node(newData);
newNode.next = this .head;
this .head = newNode;
}
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());
|
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.
Share your thoughts in the comments
Please Login to comment...