Programming Articles

Page 662 of 2544

Difference between peek(), poll() and remove() method of Queue interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 16K+ Views

This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.The peek() methodThe peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.Exampleimport java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample {    public static void main(String args[]) {       Queue queue = new LinkedList();       queue.add("Java");       queue.add("JavaFX");       queue.add("OpenCV");     ...

Read More

Check if a given array is pairwise sorted or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 212 Views

We have an array A, with n elements. We have to check whether the array is pairwise sorted or not. Suppose the array is like {8, 10, 18, 20, 5, 15}. This is pairwise sorted as (8, 10), (18, 20), (5, 15) are sorted. If the array has an odd number of elements, then the last one will be ignored.The approach is too simple, by taking I from 0 to n-1, we will see if the ith element is less than the i+1th element or not, if not, then return false, otherwise increase I by 2.Example#include #include using namespace std; bool isPairwiseSorted(int arr[], int n) {    if(n

Read More

How to remove the redundant elements from an ArrayList object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 547 Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove redundant elements of an ArrayList object −Get/create the required ArrayList.Create an empty set object.Try to add all the elements of the ArrayList object to set objectives.Clear the contents of the ArrayList using the clear() method.Now, using the addAll() method add the contents of the set object to the ArrayList again.Exampleimport ...

Read More

Difference between next() and hasNext() in java collections?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

Java provides Iterator and ListIterator classes to retrieve the elements of the collection objects.The hasNext() methodThe hasNext() method of these interfaces returns true if the collection object has the next element else it returns false.Exampleimport java.util.ArrayList; import java.util.Iterator; public class hasNextExample{    public static void main(String[] args){       ArrayList list = new ArrayList();       //Instantiating an ArrayList object       list.add("JavaFX");       list.add("Java");       Iterator it = list.iterator();       System.out.println(it.hasNext());       it.next();       System.out.println(it.hasNext());       it.next();       System.out.println(it.hasNext());    } ...

Read More

Check if a given number is sparse or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 660 Views

In this section, we will see how to check a number is sparse or not. A number is said to be sparse if the binary representation of the number, has no two or more than two consecutive 1s. Suppose a number is like 72. This is 01001000. Here no two or more consecutive 1s.To check a number is sparse or not, we will take the number as n, then shift that number one bit to the right, and perform bitwise AND. if the result is 0, then that is a sparse number, otherwise not.Example#include using namespace std; bool isSparseNumber(int ...

Read More

How to insert an object in an ArrayList at a specific position in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method accepts an element and an integer value representing the position at which we need to insert it and inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Therefore ...

Read More

How to avoid ConcurrentModificationException while iterating a collection in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 420 Views

When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to iterate its contents again it is considered that you are trying to access the collection object using multiple threads and ConcurrentModificationException is thrown.Exampleimport java.util.ArrayList; import java.util.Iterator; public class OccurenceOfElements {    public static void main(String args[]) {       ArrayList list = new ArrayList();       //Instantiating ...

Read More

Check if a number has bits in alternate pattern - Set-2 O(1) Approach in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 508 Views

Let us consider we have an integer n. The problem is to check, whether this integer has alternate patterns in its binary equivalent or not. The alternate pattern means 101010….The approach is like: calculate num = n XOR (n >> 1), now if all bits of num is 1, then the num has alternating patterns.Example#include #include using namespace std; bool isAllBitSet(int n){    if (((n + 1) & n) == 0)       return true;    return false; } bool hasAlternatePattern(unsigned int n) {    unsigned int num = n ^ (n >> 1);    return isAllBitSet(num); } int main() {    unsigned int number = 42;    if(hasAlternatePattern(number))       cout

Read More

Print first m multiples of n without using any loop in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 791 Views

In this tutorial, we are going to write a program to find out m multiples of a number n without using loops. For example, we have a number n = 4 and m = 3, the output should be 4, 8, 12. Three multiples of four. Here, the main constraint is not to use loops.We can use the range() function to get the desired output without loops. What is the work of the range() function? range() function returns a range object which we can convert into an iterator.Let's see the syntax of range().Syntaxrange(start, end, step)Algorithmstart - starting number to the ...

Read More

Check if a number is a Krishnamurthy Number or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see how to check a number is Krishnamurty number or not. A number is Krishnamurty number, if the sum of the factorial of each digit is the same as the number. For example, if a number is 145, then sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. So this is a Krishnamurty number, The logic is simple, we have to find the factorial of each number, and find the sum, then if that is the same as a given number, the number is Krishnamurty number. Let us see the code ...

Read More
Showing 6611–6620 of 25,433 articles
« Prev 1 660 661 662 663 664 2544 Next »
Advertisements