Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to find an element that appears once in an array of elements that appears three times in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;

class FindElementThatAppearsOnceInArray_CSharp
{
    public static int FindElementThatAppearsOnceInArray(int[] arr)
    {
        Dictionary<int, int> map = new Dictionary<int, int>();

        foreach (int x in arr) {
            if (map.ContainsKey(x)) {
                map[x]++;
            }
            else {
                map[x] = 1;
            }
        }

        foreach (KeyValuePair<int, int> entry in map) {
            if (entry.Value == 1) {
                return entry.Key;
            }
        }

        return -1;
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[] { 3, 5, 5, 2, 7, 3, 2, 8, 8, 3, 2, 5, 8 };

        Console.WriteLine(FindElementThatAppearsOnceInArray(arr));
    }
}




/*
run:
 
7
 
*/

 



answered Jul 8, 2024 by avibootz
0 votes
using System;

class FindElementThatAppearsOnceInArray_Csharp
{
    public static int FindElementThatAppearsOnceInArray(int[] arr) {
        int result = 0;

        for (int i = 0; i < 32; i++) {
            int sum = 0;
            foreach (int num in arr) {
                sum += (num >> i) & 1;
            }
            sum %= 3;
            result |= sum << i;
        }

        return result;
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[] { 3, 5, 5, 2, 7, 3, 2, 8, 8, 3, 2, 5, 8 };

        Console.WriteLine(FindElementThatAppearsOnceInArray(arr));
    }
}





/*
run:
 
7
 
*/

 



answered Jul 8, 2024 by avibootz
...