
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java program to print unique values from a list
To print unique values from a List in Java, the code is as follows −
Example
import java.io.*; public class Demo{ static void distinct_vals(int my_arr[], int len){ for (int i = 0; i < len; i++){ int j; for (j = 0; j < i; j++) if (my_arr[i] == my_arr[j]) break; if (i == j) System.out.print( my_arr[i] + " "); } } public static void main (String[] args){ int my_arr[] = {55, 67, 99, 11, 54, 55, 88, 99, 1, 13, 45}; int arr_len = my_arr.length; System.out.println("The distinct elements in the array are "); distinct_vals(my_arr, arr_len); } }
Output
The distinct elements in the array are 55 67 99 11 54 88 1 13 45
A class named Demo contains a function named ‘distinct_vals’ that takes in the array and the length of the array as parameters. It iterates over the array and checks for duplicate values and prints only the unique values of the array. In the main function, an array is defined and its length is stored in a variable. This ‘distinct_vals’ function is called with the defined array and length and the result is displayed on the console.
- Related Articles
- Python Program to print unique values from a list
- C# program to print unique values from a list
- Java program to print duplicates from a list of integers
- Get unique values from a list in Python
- How to subset unique values from a list in R?
- Java Program to Print all unique words of a String
- Java Program to Print the ASCII values
- Java Program to create a new list with values from existing list with Function Mapper
- Java Program to create a new list with values from existing list with Lambda Expressions
- C# program to print duplicates from a list of integers
- Python program to print duplicates from a list of integers?
- Python program to mask a list using values from another list
- Python Program to get all unique keys from a List of Dictionaries
- C++ program to print unique words in a file
- C++ Program to print unique integer pairs

Advertisements