Lonely Integer - HackerRank || HackerRank Lonely Integer Solution

Problem Name: Lonely Integer
Judge: HackerRank
Problem link: Lonely Integer



HackerRank Lonely Integer Solution in C

#include <stdio.h>

int lonelyinteger(int size, int* newArray)
{
    int i, j, x;
    for(i = 0; i < size; i++)
    {
        x = 0;
        for (j = 0; j < size; j++)
        {
            if (newArray[i] == newArray[j])
            {
                x++;
            }
        }
        if (x == 1)
        {
            return newArray[i];
        }
    }

    return 0;
}

int main()
{
    int Size, res;
    scanf("%d", &Size);
    int array[Size];
    for (int i = 0; i < Size; i++)
    {
        scanf("%d", &array[i]);
    }
    res = lonelyinteger(Size, array);
    printf("%d\n", res);

    return 0;
}


HackerRank Lonely Integer Solution in C++/Cpp

#include <bits/stdc++.h>

using namespace std;

int lonelyInteger(int size, int* array)
{
    for (int i = 0; i < size; i++)
    {
        int count = 0;
        for (int j = 0; j < size; j++)
        {
            if (array[i] == array[j])
            {
                count++;
            }
        }
        if (count == 1)
        {
            return array[i];
        }
    }

    return 0;
}

int main()
{
    int n, result;
    cin >> n;
    int numbers[n];
    for (int i = 0; i < n; i++)
    {
        cin >> numbers[i];
    }
    result = lonelyInteger(n, numbers);
    cout << result << endl;

    return 0;
}


HackerRank Lonely Integer Solution in Java

import java.util.Scanner;

public class Lonely_Integer {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = input.nextInt();
        }
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = 0; j < n; j++) {
                if (numbers[i] == numbers[j]) {
                    count++;
                }
            }
            if (count == 1) {
                System.out.println(numbers[i]);
            }
        }
    }
}





Next Post Previous Post
No Comment
Add Comment
comment url