java remove duplicates from string array

we can remove duplicates from a string array using a combination of a Set and an array. A Set is a collection that does not allow duplicate elements, so by adding the elements of the array to a Set, any duplicates will be automatically removed.

Here is an example of how to remove duplicates from a string array:

String[] array = {"a", "b", "c", "a", "d", "b"};

Set<String> set = new HashSet<>(Arrays.asList(array));

String[] uniqueArray = set.toArray(new String[set.size()]);


In this example, we first create a string array with duplicate values. We then create a new HashSet and pass the array to its constructor, which automatically removes any duplicates. We then use the toArray() method of the Set to convert it back to an array, which now only contains unique values.

It's important to note that the order of the elements in the array is not guaranteed to be preserved after removing duplicates using this method. If you want to maintain the order of the elements in the array, you can use a List or a LinkedHashSet instead of a HashSet.

Removing duplicates from a string array in Java can be achieved by converting the array to a Set, which automatically removes any duplicates, and then converting it back to an array. This method is efficient and easy to implement, and it can be used to remove duplicates from any type of array.


In Java 8, can use the Stream API to remove duplicates from a string array. The Stream API provides a functional and declarative way of processing collections of data, including arrays. Here is an example of how to use the Stream API to remove duplicates from a string array:


String[] array = {"a", "b", "c", "a", "d", "b"};

String[] uniqueArray = Arrays.stream(array)
                             .distinct()
                             .toArray(String[]::new);

In this example, we first create a string array with duplicate values. We then create a stream of the array using the Arrays.stream() method, and then use the distinct() method to remove the duplicates. Finally, we use the toArray() method to convert the stream back to an array, which now only contains unique values.

It's important to note that the order of the elements in the array is not guaranteed to be preserved after removing duplicates using this method. If you want to maintain the order of the elements in the array, you can use LinkedHashSet instead of the distinct() method, and then convert it back to the array.

In summary, in Java 8, you can use the Stream API to remove duplicates from a string array by creating a stream of the array and using the distinct() method to remove the duplicates. This method is efficient and easy to implement, and it can be used to remove duplicates from any type of array, it's a functional and declarative way of processing collections of data.

in both cases, the order of the elements in the array is not guaranteed to be preserved after removing duplicates, if you want to maintain the order you can use a List or a LinkedHashSet instead of a HashSet or use the distinct() method.

Java 8 provides multiple ways to remove duplicates from a string array using the Stream API, you can use the distinct() method or a collection such as a Set to remove duplicates, both methods are efficient and easy to implement.

sum of n numbers in c using for loop

 Here is example of a C program that uses a for loop to calculate the sum of 'n' numbers:

  1. #include <stdio.h>

  2. int main() {
  3.     int n, i, num, sum = 0;

  4.     printf("Enter the value of n: ");
  5.     scanf("%d", &n);

  6.     for (i = 1; i <= n; i++) {
  7.         printf("Enter the number: ");
  8.         scanf("%d", &num);
  9.         sum += num;
  10.     }

  11.     printf("The sum of %d numbers is %d\n", n, sum);

  12.     return 0;
  13. }

In this program, first, the user is prompted to enter the value of 'n' (the number of numbers to be added). Then, a for loop is used to iterate 'n' times and prompt the user to enter a number in each iteration. The variable 'sum' is initialized to 0 and it's being used to keep the sum of all numbers entered by the user. In each iteration, the value of 'num' is added to 'sum' using the += operator. Finally, the program prints the sum of 'n' numbers.

In this example, the for loop starts with i=1, and it will run as long as i <= n, with i being incremented by 1 in each iteration.

It's important to note that, if you want to input the n numbers at once, you can use an array and use a for loop to iterate over the array and add the numbers to the sum variable.

This C program uses a for loop to calculate the sum of 'n' numbers by prompting the user to enter a number in each iteration of the loop, adding it to a running sum, and finally printing the total sum at the end. This is a simple and efficient way to calculate the sum of multiple numbers.

what happens when a constructor is defined for an interface?

  • Interfaces in Java do not have constructors. An interface is a blueprint for a class and it cannot be instantiated. An interface defines a set of methods and variables that a class must implement, but it does not contain any implementation for those methods.
  • Java does not allow constructors to be defined in an interface, because the purpose of an interface is to define a set of methods that can be implemented by a class, and not to provide an implementation for those methods. Constructors are used to initialize an object, but since an interface cannot be instantiated, there is no object to initialize.
  • If you try to define a constructor in an interface, the compiler will throw an error: "Interface methods cannot have a body."
  • However, you can have a default method in an interface, which is a method with a defined body, but it is not a constructor.
  • In summary, constructors are not allowed in interfaces, because interfaces are used to define a set of methods that can be implemented by a class, and not to provide an implementation for those methods.
  •  In Java 8 and later versions, the concept of a default method has been introduced. A default method is a method that has a defined body and can be used to provide a default implementation for a method in an interface, but it's not a constructor.

  •  If you try to define a constructor in an interface, it will result in a compilation error.
  • Interfaces in Java do not have constructors. An interface is a blueprint for a class and it cannot be instantiated. An interface defines a set of methods and variables that a class must implement, but it does not contain any implementation for those methods.

  • When the compiler encounters a constructor definition in an interface, it will throw an error because constructors are not allowed in interfaces. The error message will typically be similar to "Interface methods cannot have a body" or "Illegal combination of modifiers: 'constructor' and 'interface'".


c program for addition, subtraction, multiplication and division using switch

 C program for addition, subtraction, multiplication and division using switch


  1. #include <stdio.h>

  2. int main() {
  3.     int num1, num2, choice;
  4.     float result;
  5.     printf("Enter two numbers: ");
  6.     scanf("%d %d", &num1, &num2);
  7.     printf("Enter your choice: \n1 for addition\n2 for subtraction\n3 for multiplication\n4 for division\n");
  8.     scanf("%d", &choice);

  9.     switch(choice) {
  10.         case 1:
  11.             result = num1 + num2;
  12.             printf("Addition: %.2f\n", result);
  13.             break;
  14.         case 2:
  15.             result = num1 - num2;
  16.             printf("Subtraction: %.2f\n", result);
  17.             break;
  18.         case 3:
  19.             result = num1 * num2;
  20.             printf("Multiplication: %.2f\n", result);
  21.             break;
  22.         case 4:
  23.             result = (float)num1 / num2;
  24.             printf("Division: %.2f\n", result);
  25.             break;
  26.         default:
  27.             printf("Invalid choice!\n");
  28.     }
  29.     return 0;
  30. }


  • the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input and is used in the switch statement. 
  • Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.
  • It's worth noting that, in the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. 
  • I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.
  • the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input using the scanf function and is used in the switch statement. Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.

  • The switch statement checks for the value of the variable choice. Depending on the value, the program enters the corresponding case and performs the operation. The break statement is used at the end of each case to exit the switch statement and prevent the program from executing the next case.
  • In the case of the division operation, I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.

  • In the default case, if the user enters any value other than 1, 2, 3, or 4, the program displays an "Invalid choice!" message. This is to handle the scenario when the user enters an unexpected value.

c program addition subtraction, multiplication and division using function

 c program for addition subtraction, multiplication and division using function

  1. #include <stdio.h>

  2. int add(int a, int b) {
  3.     return a + b;
  4. }

  5. int subtract(int a, int b) {
  6.     return a - b;
  7. }

  8. int multiply(int a, int b) {
  9.     return a * b;
  10. }

  11. float divide(int a, int b) {
  12.     return (float)a / b;
  13. }

  14. int main() {
  15.     int a = 10, b = 5;
  16.     int add_result = add(a, b);
  17.     int subtract_result = subtract(a, b);
  18.     int multiply_result = multiply(a, b);
  19.     float divide_result = divide(a, b);

  20.     printf("Addition: %d + %d = %d\n", a, b, add_result);
  21.     printf("Subtraction: %d - %d = %d\n", a, b, subtract_result);
  22.     printf("Multiplication: %d * %d = %d\n", a, b, multiply_result);
  23.     printf("Division: %d / %d = %f\n", a, b, divide_result);

  24.     return 0;
  25. }


  • In this program, four functions are defined for addition, subtraction, multiplication, and division respectively. Each function takes two integer arguments a and b, performs the corresponding operation, and returns the result. 
  • In the main function, variables a and b are initialized with the values 10 and 5 respectively.
  • The functions are called and the results are stored in variables add_result, subtract_result, multiply_result, and divide_result. 
  • These functions take two integer arguments and return the result of the operation. In the main() function, two integers are taken as input, and then the functions are called with these inputs to perform the operations and display the results.
  • In the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. 
  • It's also important to consider the case where the division operation results in decimal values, In that case, you should use float or double instead of int.

semicolon in java

 a semicolon (;) is used to separate statements in a program. Each statement in a Java program must end with a semicolon, just like in many other programming languages.

Here's an example of  semicolon in a Java program:

int x = 5; // Declare a variable and assign a value

System.out.println(x); // Print the value of the variable

x++; // Increment the value of the variable

System.out.println(x); // Print the new value of the variable

In this example, there are three statements: one for declaring a variable and assigning a value, one for printing the value of the variable, and one for incrementing the value of the variable. Each statement ends with a semicolon, which separates them from each other.

It's worth noting that, in Java, a semicolon is not always necessary. For example, if you are defining a class or method, you don't need to use a semicolon at the end of the definition. Also, you don't need to use a semicolon after a block statement (statements enclosed in curly braces {}) that is part of a control statement such as if-else, while, for, etc. The end of the block statement is represented by the closing curly brace.

In the case of a single-line if statement or a single-line loop, you can also omit the curly braces {} and write the statement directly after the if or while keyword, respectively.

if(x==5) System.out.println("x is 5");

while(x<10) x++;

In summary, a semicolon is used in Java to separate statements and mark the end of a statement. However, it's not always required and depends on the context of the statement or block.

It's worth noting that in Java, a semicolon is also used in certain control statements such as the "for" loop and the "enhanced for" (or "for-each") loop.

In a "for" loop, the initialization, condition, and increment/decrement statements are separated by semicolons. 


In this example, the initialization statement "int i = 0" is executed once before the loop starts, the condition "i < 10" is checked before each iteration, and the increment statement "i++" is executed after each iteration. Each of these statements is separated by a semicolon.

In the case of the "enhanced for" loop, the semicolon is used to separate the loop variable and the array or collection that is being iterated over.

Select Menu