Summary:
Suppose we are given with an unsorted array N positive integers.
The array has one number 'A' that is missing and one number 'B' occurs twice in the array.
Through this java code we will find these two numbers in this array.
Solution:
Explanation:
class Solve { ... }: This is a class named Solve containing a method findTwoElement which takes an array of integers arr and an integer n as input parameters. It returns an array of two integers.
int[] findTwoElement(int arr[], int n) { ... }: This line defines the method signature. It specifies that the method takes an array of integers arr and an integer n as input parameters and returns an array of integers.
int a=0, b=0;: Two integers a and b are initialized to 0. These variables will store the two elements that are repeated in the array.
for(int i = 0; i < n; i++) { ... }: This loop iterates through the elements of the array.
A ) if(arr[Math.abs(arr[i])-1]<0) { ... }: This condition checks if the element at the index equal to the absolute value of the current element minus 1 is negative. If it is, it means that this element has already been encountered before, so it's one of the repeating elements. In this case, we assign the absolute value of the current element to variable a.
B ) else { ... }: If the condition is not met, it means that the current element hasn't been encountered yet. So, we mark its corresponding index in the array as negative by multiplying it by -1.
for(int i = 0; i < n; i++) { ... }: This loop iterates through the array again to find the missing element, which is the one that is still positive.
if(arr[i] > 0) { ... }: This condition checks if the current element is positive. If it is, it means that this element is missing from the array, so we assign i + 1 to variable b.
int ans[]=new int[2];: An array named ans of size 2 is created to store the result.
ans[0]=a; and ans[1]=b;: The repeating elements a and b are assigned to the first and second positions of the ans array respectively.
return ans;: The array ans, containing the repeating elements, is returned as the result of the function.
Answered by:> >samiksha6i4j
Credit:> >Geeksforgeeks
Suggested blogs:
>Step by Step guide to Deploy Terraform in Azure using GitHub Actions
>Testing react components using Hooks and Mocks
>Use Firebase Realtime Database with ASP.NET MVC App
>Use of Singletons in .NET Core in AWS Lambda
>What are common syntax errors and exceptions in Python
>What is data binding in Angular?
Mean Stack Development
Vue JS Development
Javascript Development
React JS Development
Angular JS Development
Next JS development
Java Development
Python Development
Django Development
Cherrypy Development
C# Development
ASP.NET Development
NodeJS Development
Laravel Development
CodeIgniter Development
Zend Development
Ruby on Rails Development
CakePHP Development
PHP Website Development
Symfony Development
Drupal Development
Joomla Development
Wordpress Development
.NET Nuke Development
Kentico
Umbraco
.NET MAUI Development
Xamarin Application Development
iOS Application Development
Android Application Development
Android Wear App Development
Ionic Development
Universal Windows Platform (UWP)
Kotlin Application Development
Swift Application Development
Flutter Application Development
PWA Application Development
Flutter Health Tech & Wearable App Development Company
React Native Health Tech Wearable App Development
Offshore Software Development
Custom Application Development
Front-End Development
Full Stack Development
AI & Machine Learning
Custom CRM Solutions
Flask Software Development
Electron JS Development
ChatGPT Development
Magento Development
Magento 2.0 Development
Magento Enterprise
Shopping Cart Development
Prestashop Development
Shopify Development
Open Cart Development
WooCommerce Development
BigCommerce Development
NopCommerce Development
Virto Commerce Development
AspDotNetStorefront Development
.NET Application Development
Microsoft Dynamics CRM
VB .NET Development
Sharepoint Migration
ASP.NET Core Development
ASP.NET MVC Development
AJAX Development
Agile Development
Microsoft Bot
Microsoft Blazor
Microsoft Azure Cognitive
HTML 5
UI/UX Design
Graphic Design
Adobe Photoshop
XML Application Development
Cloud Computing Solutions
Azure Cloud App Development
AWS Development
Google Cloud Development
DevOps Consulting & Development
Kubernetes Consulting & Services
SQL Programming Development
MySQL Development
MongoDB Development
Big Data
Robotic Process Automation
Social Media Marketing
Search Engine Optimization
QA Testing
Software Testing
Software Security
Maintenance And Support
I.T. Consulting Services
Business Intelligence
YII Development
Data Analysis
Alexa Skills Development
On Demand App for Mobile repairing services
On Demand App for Car Service Booking
On Demand App for Cleaning Services
On Demand App for Pharmacy
On Demand Dedicated Developers
Nuki Smart Lock
Salto Smart Lock
TTlock Smart Lock
NFC App Development
Smart Locker Solutions
Hospital Smart Lock Systems
Hotel Smart Lock Systems
Smart Home & Office Locks
Smart Access for Schools & Colleges
Unloc Smart Lock Integration
Yale & August Smart Lock Integration
Populife Smart Lock Integration
Smart Lock Hardware Development
Agri IoT & AI Solutions
Weather & Climate Solutions
Water & Waste Management Solutions
RaspBerry Pi
Firmware Software Development
ESP 32 Software Development
Embedded Development
Internet of Things
IoT Sensor Integration & Development Solutions
Tuya IoT App Development
Particle IoT SDK
IoT Development with AI
Dairy GPS Tracking Solutions
GPS Fleet Management Software
Car Rental & Subscription Solutions
Car Buy & Sell Marketplace Development
AI-Powered Car Wash App Development
PCB Design & Fabrication
IoT AC Automation
AI–IoT Painting Solutions
IoT Wearable Hardware & App Development
HVAC Automation & AI Control Systems
Smart Home IoT Engineering
AI Embedded Systems
AI Hardware Design Service
Advanced IoT Hardware & Firmware Development
Device Driver Development Services
Microchip PIC & AVR Development
Hire IoT Architects
IoT Cloud & Infrastructure Solutions
Infineon XMC / AURIX Development Services
Matter & Thread IoT Services
Native IoT Mobile App Development (BLE & Wi-Fi)
Snapdragon IoT Firmware Development
Renesas RA/RX Firmware Services
Smart Wearable App Development
Smart IoT Meters
Smart Healthcare Wearable App Development
Health Care Monitoring System
Fitness Tracking App Development
Smart Home Automation Apps
nRF PCB Design
ESP32 PCB Design
Embedded Wearables Engineers
Rental Property Management System
Smart Lighting Development
Infineon Semiconductor Firmware Development Services
Custom Camera Development: Hardware, Firmware & PCB Prototyping
Smart Security Camera SDK
Nordic Semiconductor SDK
Infineon SDK
Arduino SDK
NFC Lock Integration
Kerong Lock Integration
IoT & AI Solutions for Manufacturing
Smart Inventory & Logistics Solution
Food & Beverage Industry Solutions
Smart Property Management
Custom Smart Home IOT SDK
Smart IoT & AI in Healthcare
AI-Powered Security Solutions
Smart Home Safety & AI
Veterinary Clinic Management (AI)
Pet Care System (AI & IoT)
Pet Training & Adoption (AI)
Healthcare IoT Development
Event Management Software
Money Remittance App
Money Lending App Development
Utility and Bill Payment App
IoT Mobile App Development (Flutter & React Native)
AI & IoT Retail Solutions
Smart EV App Development
Smart Solar IoT & AI Solutions
IoT-Based Energy Systems
Smart Energy & Utilities Solutions
IoT Security Solutions
AI-Powered Lottery App Development
AI-Sports Fitness Club Management












